Date : Converting AM to PM, and PM to AM - actionscript-3

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;

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;

Is their a simple way to detect if the actual time is near a specific time?

I've got an AS3 code that calculate and displays 4 tides.
Example :
High 1 = 13:20
High 2 = 23:30
Low 1 = 05:30
Low 2 = 16:20
In code, I'm not gonna put here the calculation as it's too long and not necessary for this problem, but the variables are :
var $pmm:String;
var $pms:String;
var $bmm:String;
var $bms:String;
High 1 = $pmm
High 2 = $pms
Low 1 = $bmm
Low 2 = $bms
The app also displays the actual time
var my_timer:Timer=new Timer(1000);
my_timer.addEventListener(TimerEvent.TIMER, onTimer);
my_timer.start();
function onTimer(e:TimerEvent):void {
now = new Date();
trace(now.hours + ":" + now.minutes);
}
actual_time.text=now.hours + ":" + now.minutes;
Is there a simple way to detect if now.hours is closest to $pmm, $pms, $bmm or $bms ?
Not sure if this would be the best method, but I would take the difference between the now time and the tide times and push it into an array like so:
var tideArray = new Array();
tideArray.push({tide:"pmm", difference: 3});
tideArray.push({tide:"pms", difference: 5});
tideArray.push({tide:"bmm", difference: 0});
tideArray.push({tide:"bms", difference: 8});
tideArray.sortOn("difference", Array.NUMERIC);
trace(tideArray[0].tide);
The trace of the first element would return the lowest difference in time, which in this case would be bmm.

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!

date conversion and math in actionscript 3.0

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);

TimeDelta class in ActionScript?

Is there any ActionScript class which represents "durations of time", similar to the TimeDelta class in Python?
Edit: Thanks for the responses. I should clarify a bit, though: I want be able to ask questions like "how many weeks are between date0 and date1" or "let x represent "one day". What is date2 + x?"
I know I can do all this by representing dates as timestamps... But I'm hoping to find something nicer.
I posted a full AS3 port of the .NET TimeSpan class on this question, which sounds exactly like what you need.
// 5 days from new
var ts : TimeSpan = TimeSpan.fromDays(5);
var now : Date = new Date();
var fiveDaysTime : Date = ts.add(now);
// Diff between dates
var d1 : Date = new Date(2009, 1, 1);
var d2 : Date = new Date(2009, 1, 6);
var ts : TimeSpan = TimeSpan.fromDates(d1, d2);
You can use time() in the Date class to get unix era milliseconds and use that for time delta.
If you subtract two dates:
var dateDiff = date1 - date2;
dateDiff will hold the number of milliseconds between the two dates. You can then convert from milliseconds to whatever useful number you like.
I don't think there is a class which measures change in time in Actionscript 3. According to this blog post on Adventures in Actionscript, timing is very inaccurate in the Flash player on the web. That post is pretty informative and has a class called SuperTimer that might help you. You might want to keep this inaccuracy in mind if using solutions posed by Justin Niessner and toastie.