splite string using mutilple characters - actionscript-3

what I need is simple thing, I have string which cantains data - time retrived from mySQL in mySQL format HH:MM:SS YY-MM-DD what I need is to split this string in actionscript to array of numbers like this
HH
MM
SS
YY
MM
DD
so I can compare it with current time, any one know how to splite using multiple delimiters at first, then compare it with current time. this is my work until now
var param:Array = datetime.split(" :-");
var currentTime:Date = new Date();
var seconds:uint = currentTime.getSeconds();
var minutes:uint = currentTime.getMinutes();
var hours:uint = currentTime.getHours();
var days:uint = currentTime.getDay();
var monthes:uint = currentTime.getMonth();
var years:uint = currentTime.getFullYear();
if(int(param[3]) > years)
return years + " سنة ";
if(int(param[4]) > monthes)
return monthes + " شهر ";
if(int(param[5]) > days)
return days + " يوم ";
if(int(param[0]) > hours)
return hours + " ساعة ";
if(int(param[1]) > minutes)
return minutes + " دقيقة ";
if(int(param[2]) > seconds)
return seconds + " ثانية ";
return param[0] + " يوم ";
`

Split allows the delimiter to be a regexp, so you can say this or that. Something like this:
myStr.split(/:|-/)
Good luck!

You can use multiple characters, with separated by pipe |
myStr.split(/:|-|[|\(|\)]/)
make sure to use \ if you use ( ) and similar, so ( )

To solve your specific question, you might consider replacing the characters to be the same, and then split on that one. Use datetime.replace(/[ :-]/g, "|") and then split on "|". (I didn't check the correctness of the regexp). What Tyler says is more elegant: datetime.split(/[ -:]/). I stand for the rest though:
What MySQL outputs (via php?) is a standard date notation. You could try and use the Date.parse(dateString) to get a timestamp from it, and convert that into a date object by passing it as the sole constructor parameter:
recordedTime = new Date(Date.parse(datetime));
You could then compare the two date objects directly.
if (recordedTime.getFullYear() > currentTime.getFullYear()) { ... }
Hope it helps.

Related

Convert DDMMYY to DD-MM-YY in SSIS Derived Column

I am facing issue while placing "-" in a varchar datatype.
. Need is after last two digits we need to put "-" and then again after two digits and so on.
Input String is- 21220 Output String- 2-12-20
Or,
Input String- 311220 Output String- 31-12-20
Can anyone help me on this ?
I would go with a script component for string manipulation because it's much easier to do and it's more readable.
1) Add a script component as a transformation
2) Under input columns, check your date column (I called this myDate in the example below)
3) Under Inputs and Outputs, under Output 0, add a column for the new date, called formattedDate below
4) In the script, modify the Input0_ProcessInputRow method. Before parsing the string, make sure it's not null. Then pad it with a "0" on the left to insure that we always have a length of 6. Parse, the date elements and set the value for the new column.
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (!Row.mydate_IsNull)
{
var paddedDate = Row.mydate.PadLeft(6, '0');
var day = paddedDate.Substring(0, 2);
var month = paddedDate.Substring(2, 2);
var year = paddedDate.Substring(4, 2);
Row.formattedDate = $"{day}-{month}-{year}";
}
}
(DT_WSTR, 2)(DT_I4)LEFT(RIGHT("0" + #[User::date], 6), 2)
+ "-" + SUBSTRING(RIGHT("0" + #[User::date], 6), 3, 2)
+ "-" + RIGHT(#[User::date] , 2)

Comparing 2 strings from HTML file - Angular

I am using angular 7 and I would like to know how to compare two strings. In this case, each of my strings simulates one date, let's say "2019-12-26" and "2018-12-26".
In Javascript is pretty simple to compare them since I just need to use the operators:
console.log(today > "2018-12-06");
It is working how I supposed it was gonna work. It basically returns true. Nevertheless, I am trying to do exactly the same from my HTML file
<div *ngIf="today > example.endDate">
being today and 'example.endDate' two strings containing exactly the same strings that I used for the Javascript example, but it does not show any of them.
Is there any other way to make this comparison?
Regards,
Mario
UPDATE
I have had a second look at the problem and it seems that the comparison is not a problem, but the way of getting the variable is.
I get a variable in ngOnInit().
ngOnInit() {
this.getCurrentDate();
}
//Get current date
getCurrentDate() {
let aux = new Date();
//2 characteres
let dd = String(aux.getDate()).padStart(2, "0");
let mm = String(aux.getMonth() + 1).padStart(2, "0"); //January is 0!
let yyyy = aux.getFullYear();
let today = yyyy + "-" + mm + "-" + dd;
let other = "2019-01-31";
}
The problem is that I use this variable directly in my HTML how I previously showed. The error I get is the following:
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression
has changed after it was checked. Previous value: 'ngIf: undefined'.
Current value: 'ngIf: true'.
So the problem is that I am using a variable in the HTML file before getting the value. Or at least it is what I understand
To check this error I have created a Stackblitz representation. On it, I have created two examples:
Variables not on ngOnInit()
Variables in ngOnInit()
https://stackblitz.com/edit/angular-jjgsmq?file=src%2Fapp%2Fhello.component.ts
the most simple solution is just to
and are you sure that ngOnInit is the right LifeCycle hook for you?
I would try ngAfterContentInit() if the component is "heavy" to render other wise ngAfterViewInit() would have been my choice
<div *ngIf="IsTodayBigger()">
ngOnInit() {
this.getCurrentDate();
}
IsTodayBigger(): boolean {
today=this.getCurrentDate()
exampleEndDate= example.endDate;//use binding or ViewChild if needed
return today&&exampleEndDate&& today> example.endDate
}
//Get current date
getCurrentDate() {
let aux = new Date();
//2 characteres
let dd = String(aux.getDate()).padStart(2, "0");
let mm = String(aux.getMonth() + 1).padStart(2, "0"); //January is 0!
let yyyy = aux.getFullYear();
today = yyyy + "-" + mm + "-" + dd;
let other = "2019-01-31";
}

Chrome cannot convert date string to date in format YYYY/MM/DD PM hh:mm

I think Chrome is most flexible browser but it is not working in below case:
newrecorddate = "2015/10/20 PM 06:09"
var d = new Date(newrecorddate);
console.log("d="+d);
In IE11 the date (d) is returned successfully. In Chrome "Invalid Date" is returned. How can I workaround it?
UPDATE:
Dai's code actually solve the problem so it is marked as answer. Here is the code I use:
var r = /(\d{4})\/(\d{1,2})\/(\d{1,2}) (PM|AM) (\d{2})\:(\d{2})/;
if( newRecordDate.match( r ) ) {
var ymd = newRecordDate.split(" ")[0];
var tt = newRecordDate.split(" ")[1];
var tod = newRecordDate.split(" ")[2];
var d = new Date( ymd + " " + tod + " " + tt );
}
return d;
(Disclaimer: I worked on Chakra, Microsoft's JavaScript engine)
ECMAScript's specification does not list the formats that Date's constructor must successfully parse, in practice most implementations will generally successfully read almost every non-ambiguous format available, however the format you're using, YYYY/MM/dd tt HH:mm is not a format seen in reality (the tt is in the middle instead at the end). A good heuristic implementation might be able to guess it but it helps to not have to guess or depend on any feature support not present in the language's specification.
You'll have to parse the date yourself, reformat it, and pass that into Date's constructor, fortunately regex makes this easy:
var r = /(\d{4})/(\d{2})/(\d{2}) (PM|AM) (\d{2})\:(\d{2})/;
if( r.match( newRecordDate ) ) {
var ymd = newRecordDate.substr( 0, 10 ); // note substr instead of substring
var tt = newRecordDate.substr( 11, 2 );
var tod = newRecordDate.substr( 14, 5 );
var d = new Date( ymd + " " + tod + " " + tt );
}
That should work. Untested though.

Code gives out "strange output"

I am haveing some trubble witht this code. My output will look somthing like this. "29 days, 29 days, 0 hours, 29 minutes". The only variable outside of the code is toon.lastModified. it can be somthing like 1431769709000 . The task for the code is to tell how mutch time that have passed from now, untill there was a change in the JSON. I got the feeling that the problem is that + is used to add numbers and to set strings into each other.
var nowTime = new Date()
var timeStamp = nowTime.getTime()
var lastModTS = ((timeStamp-toon.lastModified)/1000).toFixed(0)
var lastModS = (lastModTS%60)
var lastModMin = (((lastModTS-lastModS)/60)%60)
var lastModH = (((lastModTS-(lastModS+lastModMin*60))/60)%24)
var lastModDay = (Math.floor((lastModTS-(lastModS+lastModMin*60+lastModH*60^2))/(24*60^2)))
var sinceLastMod=""
if (lastModDay==1){
sinceLastMod=lastModDay+" day, "
} else if (lastModDay>1){
sinceLastMod=lastModDay+" days, "
} if (lastModH==1){
sinceLastMod=sinceLastMod+lastModH+" hour, "
} else if (lastModH==0&&lastModDay==0){
sinceLastMod=""
} else {
sinceLastMod=sinceLastMod+sinceLastMod+lastModH+" hours, "
} if (lastModMin==1){
sinceLastMod=lastModDay+" minute, "
} else if (lastModMin==0&&lastModH==0&&lastModDay==0){
sinceLastMod=">1 minute"
} else if (lastModMin>1){
sinceLastMod=sinceLastMod+lastModMin+" minutes"
}
return sinceLastMod //it does not look like this in the code, as it is retuned in an array
EDIT
found another thing
at the end of lastModDay, Google would not do the expotensial, i had to wright it to 60*60 for it to get it.
This line:
sinceLastMod=sinceLastMod+sinceLastMod+lastModH+" hours, "
Should be:
sinceLastMod=sinceLastMod+lastModH+" hours, "
Remove one of the sinceLastMod variables. You've got it in there twice.

Regarding formatting of the query

I have made an application in which I have put the sql queries in java method as below..
private static String getSaleTransactionsQuery(final String currentTradingDate){
final String query =
" SELECT '990' line_code, " +
" sum(toti_wag.TTIW_CUST_CNT) amount " +
" FROM total_till_wag toti_wag " +
" where toti_wag.TOTI_TRADING_DATE = '" + currentTradingDate + "' ";
return query;
}
Now when I want to filter out sql queries then I have to copy this query on the sql file and have to manually remove the quotes("") and the plus(+) sign in order to make them as pure sql and then have to execute them to see the query output , is there any other tool which will format all these means remove the quotes and the plus sign ,please advise
You could make a simple script to help you.
http://jsfiddle.net/GFhFg/7/
I found it pretty easy to throw something together in JavaScript that does the job.
function cleanIt(txt) {
var result = "";
// remove semi-colon at the end
txt = txt.replace(/\;$/m, '');
var lines = txt.split('\n');
for(var i=0; i<lines.length; i++) {
var line = lines[i];
// trim whitespace
line = line.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
// remove + from beginning or end of line
line = line.replace(/^\+\s*/, '').replace(/\s*\+$/, '');
// remove quotes
line = line.replace(/"/g, '');
// remove the +'s around parameters/variables
line = line.replace(/('?\s*\+\s*)([^\s\+']+)(\s*\+\s*'?)/g, '$2')
result += line + '\n';
}
return result;
}
Obviously it hasn't be exhaustively tested, but it should give you a base to start from.
There is no automated way. You may write some generic custom function to do it for you.
One good proactive is to log the required details in log files. If you do and log your query then you will see your query without quotes and plus signs in the log file.