date conversion and math in actionscript 3.0 - actionscript-3

I have two values coming in, a string in the form StartTime YYYY-MM-DD HH:MM:SS and a Duration in the form HH:MM:SS. from these two values I need to determine the endTime and put it in the same format as the StartTime.
I have tried to make something like
startTimeArray:Array = StartTime.split(/[ -:]/);
var date:Date = new Date(startTimeArray[0], startTimeArray[1]-1, startTimeArray[2], startTimeArray[3], startTimeArray[4], startTimeArray[5] );
but the split of course results in strings and not numbers and I can't figure how to convert all these sections of the time into numbers, and following that I do not see a way to apply math by adding the Duration Date object to the StartTime Date object if I am able to get them properly converted.
Am I going down a good path here? if so, how can I convert an array of strings to numbers and how can I add the duration to starttime? Thanks

const start:String = "2011-04-03 01:39:48";
const startArray:Array = start.split(/[: -]/g);
var startDate:Date = new Date(
startArray[0], startArray[1]-1, startArray[2],
startArray[3], startArray[4], startArray[5]
);
const duration:String = "02:10:10";
const durationArray:Array = duration.split(/:/g);
var durationDate:Date = new Date();
durationDate.setTime(
durationArray[0] * 3600000 + /* hour = 60m * 60s * 1000ms */
durationArray[1] * 60000 + /* minute = 60s * 1000ms */
durationArray[2] * 1000 /* second = 1000ms */
);
var finalDate:Date = new Date();
finalDate.setTime(startDate.time + durationDate.time);
trace(startDate);
trace(durationDate.time);
trace(finalDate);
Outputs:
Sun Apr 3 01:39:48 GMT-0300 2011
7810000
Sun Apr 3 03:49:58 GMT-0300 2011

You will want to split the date and time first and then work from there with the arrays.
var date_:String = StartTime.substring(0,StartTimet.indexOf(" "));
var time_:String = StartTime.substring(StartTime.indexOf(" ")+1, StartTime.length);
trace(date_); // YYYY-MM-DD
trace(time_); // HH:MM:SS
Then split your date into an array
var date_array:Array = StartTime.split("-");
Cast the elements of the array with Number
Then do the same for time but use ":" for the split.
Duration can be split the same way. Then just use normal addition after casting.
var d:Date = new Date(year,month,date,hour,minutes,seconds);
var d_added:Date = new Date(year,month,date,hour+duration_hour,
minutes+duration_minutes,seconds+duration_seconds);

Related

How to set a start value for a primefaces extension timer

I use a primefaces extension timer with the following code :
<pe:timer
style="color:darkgrey;"
timeout="1000"
forward="false"
format="HH:mm:ss"/>
But I have a start value. The previous code start from the value 00:00:00, but I have a start time from a Java bean. I have a Date object, or date as long type (from 1970) which is the value from I want to start.
For example I got 1548434800083 or 17:47 25/01/2019 and I want to display the time between this date and now. So how I can set my start value with this date ? I got milliseconds but I can get seconds instead of.
The solution is the following, I have a Java bean, that contain a date in millisecond. I get it with the function getTime() from the Java class Date.
I display my counter, chronometer with the primefaces code :
<pe:timer
style="color:grey;"
forward="true"
formatFunction="return displayElapsedTime('#{synopticBean.longEnteredTime}');"/>
That code call a javascript function, that return a format counter/chronometer :
function displayElapsedTime(longEnteredTime){
var now = new Date();
var elapsedTime = now.getTime() - longEnteredTime;
var numHours = Math.floor(elapsedTime / 3600000);
var minutesAndSecondMS = elapsedTime - numHours;
var minutes = Math.floor((minutesAndSecondMS % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((minutesAndSecondMS % (1000 * 60)) / 1000);
var innerHtmlText = (('0' + (numHours)).slice(-2) + ":" +('0' + (minutes)).slice(-2) + ":" + ('0' + (seconds)).slice(-2));
return innerHtmlText;
}
It display a duration, the number of hours. I only count with hours, no day.
Best solution for your case is formatting. But sometime we need to set start value of primefaces timer:
Run javascript:
PF('timer').currentTimeout = 100;

Highcharts with external CSV $.get - No xAxis date

I'm trying to create a spline chart using this CSV:
slave_id,date,time,rtc_temp,temp1,temp2,temp3
1,2017/12/26,16:42:59,21,11.50,13.13,5.88
2,2017/12/26,16:43:29,21,14.13,20.63,99.99
1,2017/12/26,16:44:00,21,11.50,13.13,5.88
2,2017/12/26,16:44:30,21,14.13,20.63,99.99
1,2017/12/26,16:45:01,21,11.50,13.13,5.88
2,2017/12/26,16:45:31,21,14.13,20.63,99.99
1,2017/12/26,16:46:02,21,11.50,13.13,5.88
2,2017/12/26,16:46:32,21,14.13,20.63,99.99
As you can see here [IMAGE], the graph is showing the date and time, but the x Axis is not accepting the date / time.
Ive tried using date.UTC, but that did not work either. Can someone point me in the right direction?
https://jsfiddle.net/asvoy6b9/ [not working due to CSV missing]
Full code [Hastebin]
I see that date variable in your code is a string:
// all data lines start with a double quote
line = line.split(',');
date = line[1] + " " + line[2];
(...)
RTC.push([
date,
parseInt(line[3], 10)
]);
If you choose to construct the point's options as an array of two values and the first value is a string then it's treated as its name property (not x).
Explanation: https://www.highcharts.com/docs/chart-concepts/series
In that case Highcharts assigns subsequent integers as x values for all points (that's why there're values like 00:00:00.000 (1 Jan 1970), 00:00:00.001 etc.).
You need to parse your date to timestamp. You can use Date.UTC() (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC) or some other function for this.
I've managed to get it working with Date.UTC using the following code:
var yyyymmdd = line[2].split("-"); //Split the date: 2017 12 16
var hhmmss = line[3].split(":"); //Split the time: 16 11 14
var date = Date.UTC(yyyymmdd[0], yyyymmdd[1] - 1, yyyymmdd[2], hhmmss[0], hhmmss[1], hhmmss[2]); //Stitch 'em together using Date.UTC

Action script for date

I am an freelance animator in flash with very limited knowledge in AS3. I make animation videos in swf format and sell it for money. Most of the time, my clients give me a small script and ask for a sample based on the script without any watermark or logo embeded. I have to oblige in order to win the chance of getting the work. Most of the time, the client does not respond after they get the sample and I think that they are getting their work done for free.
I want to embed a script in AS3 in such a way that the video will play for only a pre-defined number of days( eg 3 days ) or till a particular date.
Please help
you have to put your animation in one MovieClip.
I named it 'animation', then set your expire date, I commented where to set
function getDaysBetweenDates(date1:Date, date2:Date) : int {
var oneDay:Number = 1000 * 60 * 60 * 24;
var date1Milliseconds:Number = date1.getTime();
var date2Milliseconds:Number = date2.getTime();
var differenceMilliseconds:Number = date1Milliseconds - date2Milliseconds;
return Math.round(differenceMilliseconds/oneDay);
}
var year : int = 2014; //here you put the year, month and the day when you want to expire
var month : int = 12;
var day : int = 28;
var daysRemaining = getDaysBetweenDates(new Date(year, month-1, day+1), new Date());
timeTXT.text = daysRemaining.toString(); //you can create dinamyc text and show to your client how much time he has befor the animation expire
if(daysRemaining <= 0) {
animation.visible = false; //here we make animation movieclip invisible if is expired
}
try it!
Hope this help!

Send date to AS3 from html

I'm trying to make a countdown timer, where I could easily set the endDate variable in html...
I can send strings to AS3 with flashvars="endDate=123", but how do I make it to be date (for example christmas)? - I need it to be date because then in AS3 I have something like endDate - todayDate = timeRemaining
Just pass date to flash vars using formats specified here and parse that string in AS3 using Date.parse() method.
In this example you get a countdown in days to Xmas.
var dateFromFlashVar : String = "2013/12/25 15:30:20 GMT+0300";
trace( new Date( new Date( Date.parse( dateFromFlashVar ) ).time - new Date().time ).getDate().toString() );
to transfrom a unix_timestamp date to as3 Date, you can do:
// unixTimeStamp is in seconds so we multiply by 1000 because flash Date take miliseconds
var date:Date = new Date( unixTimeStamp*1000 );
to get back unix timestamp of a date you can do:
// divide by 1000 to get seconds instead of miliseconds
var timeStamp:int = date.time / 1000;
i hope this is what you where searching for

Date : Converting AM to PM, and PM to AM

Perhaps a stupid question, but i really dont know the answer :(
lets say i have a date object, how can i change its AM date to PM, or vice verca?
Thanks
seems like you can
var currentTime = new Date();
var hours:uint = currentTime.getHours();
then you can say, if hours is greater than or equal to 12, then subtract by 12, otherwise, add 12 to it, for example, by
public static const millisecondsPerHour:int = 1000 * 60 * 60;
var reversedAMPM = new Date(currentTime.getTime() + (12 * millisecondsPerHour));
Although Jian's version should work, here's an alternative.
Assuming var date:Date is initialized, you may literally change it:
date.hours += (date.hours > 12) ? -12:12;