开发者

Q;Determine date in YYYY-MM-DD HH:MM:SS format

开发者 https://www.devze.com 2022-12-19 17:19 出处:网络
I need to be able to subtract 2hrs ,8hrs, 1 day and 1 week from the current Date. Then convert to yyy-mm-dd hh:mm:ss format.

I need to be able to subtract 2hrs ,8hrs, 1 day and 1 week from the current Date.

Then convert to yyy-mm-dd hh:mm:ss format.

So far I have been unsuccessful.

What 开发者_如何学Gois the proper method to do this in actionscript?


There are a few options here, but I think the simplest solution in your case is to work with milliseconds. You can use += and -= to modify the current millisecond value of the date. The trickiest thing is the conversion of your values to milliseconds. Here are a few examples:

var myDate:Date; //assuming this is a actual date value.

//subtract 2 hrs
var twoHoursInMilliseconds:int = 2 * 60 * 60 * 1000; //2 hours * 60 minutes * 60 seconds * 1000 milliseconds (in a second)
myDate.milliseconds -= twoHoursInMilliseconds;

//subtract 1 day
var oneDayInMilliseconds:int = 1 *24 * 60 * 60 * 1000;
myDate.milliseconds -= oneDayInMilliseconds;

For formating, you will want to use this methods:

trace(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());

Hopefully that points you in the right direction,

Happy Coding!

Edit: Updated code to fix bug.


I will agree with Tyler for his approach to work with date in the form of miliseconds. For conversion, you may also like to use DateFormatter as below.

var dateFormatter:DateFormatter = new DateFormatter(); dateFormatter.formatString = 'yyy-mm-dd hh:mm:ss' ; var formattedDate:String = dateFormatter.format(d); trace(formattedDate);

Wishes, Ashine.


Just change the UTC values of the date/hour.

var d:Date = new Date();
d.dateUTC -= 1; //subtract one day
d.hoursUTC -= 1; //subtract one hour

and then trace the output using Tyler's trace statement.

trace(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());

For more information on the Date class, check out http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Date.html

0

精彩评论

暂无评论...
验证码 换一张
取 消