TimeDelta class in ActionScript? - actionscript-3

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.

Related

How to write a Quartz timer service that updates every 6 months?

I want a Quartz timer service for the update code below. Every six months, all users' keys should be updated according to their creationDate:
//dataTypes are date only
List<User> allDbUsers=userRepository.findAll();
Date currDate=new Date();
final long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
for(User user:allDbUsers)
{
Date creationDate=user.getCreationDate();
String firstKey=user.getFirstKey();
Long diffInDays=(currDate.getTime()-creationDate.getTime())/DAY_IN_MILLIS;
if(diffInDays==180)
{
user.setSecondKey(firstKey);
user.setFirstKey("aH&#KK");
userRepository.saveAndFlush(user);
}
}
You can use Calendar for view diff between dates, for example:
Calendar currentDate = Calendar.getInstance();
Calendar sixMonthAfterCreation;
for (User user : allDbUsers) {
sixMonthAfterCreation = Calendar.getInstance()
sixMonthAfterCreation.setTime(user.getCreationDate());
sixMonthAfterCreation.add(Calendar.MONTH, 6);
String firstKey = user.getFirstKey();
if (currentDate.after(sixMonthAfterCreation)) {
user.setSecondKey(firstKey);
user.setFirstKey("aH&#KK");
userRepository.saveAndFlush(user);
}
}
or if you use java 8 you can convert you creation date to LocalDate and view diff.
If I right understand you, you want use Quartz Scheduler. I did not use it but I think you need implement org.quartz.Job, override execute method and write code for updating users in it.
For more information about Quartz Scheduler look this question
One clarification this code not for run scheduler, it code for method job.excecute(what job must do). Scheduler should run every midnight(for example), then every midnight will be called execute method which find all user for update and do it.

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

Start or End of week in Actionscript

I am looking for a way of getting the start of the week relative to today in Actionscript using (and currently stuck with) Flex 4.1. In Java I would do this using the Calendar class via:-
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DAY_OF_WEEK,
1);
return cal.getTime();
I can't seem to find any solution that is anywhere as elegant - am I missing something?
There are two classes, of the same name, DateTimeFormatter. One is a pure Actionscript class, the other is a new class added to Flex 4.5 that wraps the Actionscript version. The Flex version does some of the heavy lifting for you.
There is a getFirstWeekday() method, it will be respective of the locale you specify.
[Edit]
I got carried away, didn't answer your question specifically. I'm not aware of anything more elegant than the DateTimeFormatter. But from there you can use a Date object to get the current time, and get what you're after.
There are a handful of date utilities, but I'm not aware of this particular solution. Are you looking for something like this:
var timeFormatter:DateTimeFormatter = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.NONE, DateTimeStyle.SHORT);
var firstDay:int = timeFormatter.getFirstWeekday()
var now:Date = new Date();
var dayDelta:int = now.day - firstDay;
var firstDayInMillis:Number = now.time - (dayDelta * 86400 * 1000);
// you can construct a new Date from here: var first:Date = new Date(firstDayInMillis);

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;