Add days to Date in ActionScript - actionscript-3

We have an application in which the user has to enter a date who's value is no more than 30 days after the the current date (the date on which the user uses the application). This is a Flash application, therefore I need a way to add 30 days to the current date, and get the right date. Something like in JavaScript:
myDate.setDate(myDate.getDate()+30);
Or in C#:
DateTime.Now.Add(30);
Is there such a thing in ActionScript?

While the other answers will work im sure, it is as easy as doing:
var dte:Date = new Date();
dte.date += 30;
//the date property is the day of the month, so on Sept. 15 2009 it will be 15
This will even increment the month if necessary and year as well. You can do this with the month and year properties as well.

I suggest that you look here: How can you save time by using the built in Date class?.
It should be something like this:
var date:Date = new Date();
date.setDate(date.date + 30);

My TimeSpan class might prove useful here (it's a port of the .NET System.TimeSpan):
var now : Date = new Date();
var threeDaysTime : Date = TimeSpan.fromDays(3).add(now);

#Zerata
Adding milliseconds directly will not work if dates are across day light saving change...
However, you can add seconds directly:
var date: Date = new Date();
date.seconds += 86400;
=> this works even if dates are across DLS change.
Maurice

I'm writing the code from the top of my head, without compiling it, but I'd use getTime(). Something like:
var today : Date = new Date();
var futureDate : Date = new Date();
futureDate.setTime(today.getTime() + (1000 * 60 * 60 * 24 * 30));
1000 * 60 * 60 * 24 * 30 = milliseconds * seconds * minutes * hours * days
Makes sense?

Related

Flash AS3 get time in another timezone

I need to make a countdown clock to a certain time in New York in Flash AS3, regardless of the user's local machine time. I can do the clock itself once i have the Date object correct, but I can't seem to figure out how to create a Date object with the time in New York. I'll have to keep in mind Daylight Savings Time as well.
any help is appreciated. thanks.
after yet another google search, i finally found the solution here.
var now:Date = new Date();
trace("now local time: " + now);
var newYorkOffset:int = 1000 * 60 * 60 * 5; // 5 hours offset for NYC
var localTimezoneOffset:Number = now.getTimezoneOffset() * 60 * 1000;
// add now + localTimeZoneOffset to get UTC, then subtract NY offset to get NY time
now.setTime(now.getTime() + localTimezoneOffset - newYorkOffset);
trace("now in New York: " + now);
and i'm pretty sure that this takes daylight savings into account, since getTimezoneOffset() includes daylight savings automatically based on the date.
to test, temporarily change your machine's timezone to something other than New York. "now in New York:" time will not change.

Actionscript 3 : get time to specific timezone (not the computer one)

I have an UTC timestamp and I would like to display the corresponding date and hour in a specific timezone (e.g. France local time) which is not the local timezone of the computer which might be in US. It seems complicated to take into account Daylight saving time.
On Flash/as3 documentation, I only found the Date class which have no function to specify the timezone (only use local time or UTC).
If I understand your problem, flash.globalization.DateTimeFormatter is your solution.
The DateTimeFormatter class provides locale-sensitive formatting for
Date objects and access to localized date field names. The methods of
this class use functions and settings provided by the operating
system.
Here's a function i found somewhere, probably right on stack oveflow to check if daylight savings is in effect.
public static function isObservingDTS(): Boolean {
var winter: Date = new Date(2011, 01, 01); // after daylight savings time ends
var summer: Date = new Date(2011, 07, 01); // during daylight savings time
var now: Date = new Date();
var winterOffset: Number = winter.getTimezoneOffset();
var summerOffset: Number = summer.getTimezoneOffset();
var nowOffset: Number = now.getTimezoneOffset();
if ((nowOffset == summerOffset) && (nowOffset != winterOffset)) {
return true;
} else {
return false;
}
}
flex will keep a date in UTC and a timezone offset. any displaying of the date will show the timezone corrected form of the date, unless you calculate the new time and spit the date out as a string. something like this
private function convertToTimezone(dtDate: Date, timezoneOffset: Number = 0): String {
//timezoneOffset in minutes
dtDate.setTime(dtDate.getTime() + (timezoneOffset * 60000) + (isObservingDTS() ? (60 * 60 * 1000) : 0));
return dtDate.toUTCString();
}
not very elegant, but it should get you there.

Trying to subtract 5 days from a defined date - Google App Script

I'm trying to write a script which is supposed to send out an email and create two calender entries when submitting a form. To be honest, this is my first script and I am very happy that the email is send out and the calender entries are working as well. The thing which gives me a headache is to subtract 5 days (actually x days) from a defined date.
First I thought I could simply do something like
var liveday = e.values[2];
var newday = liveday-5;
well, this didn't work :-)
I tried more:
var newtime = new Date(liveday);
var yr = newtime.getYear();
var dt = newtime.getDay();
var mt = newtime.getMonth();
var dtnew = dtnew.setDate(mt, dt-5, yr);
But here I received 1418256000000 whereas liveday = 12/01/2014. Not sure why days were added, rather than subtracted.
I am quite confused here and the answer can't be that hard.
I just want to subtract 5 days from 12/01/2014 to receive 11/27/2014.
Thanks for having a look
the comment sends you to a rather complicated serie of codes... there is a far more simple way to get that, here is the code :
function test() {
Logger.log('today= '+new Date()+' and 5 days ago is '+subDaysFromDate(new Date(),5));
}
function subDaysFromDate(date,d){
// d = number of day ro substract and date = start date
var result = new Date(date.getTime()-d*(24*3600*1000));
return result
}
Logger result :
[13-11-18 23:39:50:364 CET] today= Mon Nov 18 2013 23:39:50 GMT+0100 (CET) and 5 days ago is Wed Nov 13 2013 23:39:50 GMT+0100 (CET)
if you want to get the date in the form dd/mm/yyyy use Utilities.formatDate(date, timeZone, 'dd/MM/yyyy), see doc here

Calculate time range and parse date

I have simple spreadsheet and need to calculate time ranges, according to the image below:
My rule is basically a simple math operation: (end date - initial date) - "09:00:00".
My result column gives me something like: -0:21:00 and it's correct.
I have my columns formatted as "hour" (Format > Number > Hours). I need to get the sum up of third column and send the result through e-mail. Part of the code was intentionally omitted. Below my script snippet:
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var to = row[0]; // e-mail
var total = row[1]; // total column
msg = "Total hours: " + Utilities.formatDate(total, "GMT-0300", "HH:mm:ss");
subject = "Test";
MailApp.sendEmail(to, subject, msg);
}
When I try to format Utilities.formatDate(...) before sending e-mail, the hour goes wrong. I've tried to remove Utilities call, passing the hour directly, but it gives me the following result:
Fri Dec 29 1899 23:45:28 GMT-0300 (BRT)
I realized where is my total var must be a JavaScript Date object, but it didn't work also. I need that my result column were send by e-mail.
I would dissociate the presentation in the spreadsheet from what you need to send in the email.
The result you get that in your column is displayed correctly, but represents the offset in milliseconds (displayed in hours) from the 1st January 1900 at 00:00:00 GMT.
So when you convert that to a date that is what you get.
Instead, that the number of milliseconds between the two dates and use a function that will return a 'human readable' number ... such as the one described here:
Convert time interval given in seconds into more human readable form
Good Luck
Solved my problem with a simple approach and Math.round(...) makes it quite acceptable:
function getReadableTime(ms) {
var spreadsheetTimezone = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
var dateString = Utilities.formatDate(ms, spreadsheetTimezone, 'EEE, d MMM yyyy HH:mm:ss');
var date = new Date(dateString);
var epoch = new Date('Dec 30, 1899 00:00:00');
var diff = date.getTime() - epoch.getTime();
x = diff / 1000;
seconds = Math.round(x % 60);
x /= 60;
minutes = Math.round(x % 60);
x /= 60;
hours = Math.round(x % 24);
x /= 24;
days = Math.round(x);
return createMsg(hours, minutes);
}
The function createMsg(...) is omitted because it takes the parameters and create a readable message to the user. The trick here is creating a time range in ms and follow one of the rules to make it seconds/minutes/hours/days or whatever you need.

Get date from period number in Actionscript

I have a table which containt a date, a number for the number of weeks per period, and a year. the user then enters a date and I can calculate the period number from this. But I'd like to do it the other way too: Entering a period number and get the start and end date of the period from this. Unfortunately, I can't seem to get the right logic. Could anyone guide me with this?
Thank you.
EDIT:
options[0] being the start date from the database and options[1] the number of weeks for one period.
This is the function I already have and which works:
private function dateToPeriod(date:Date):Number
{
var d = new Date(options[0]);
var periode = Math.floor((date.time - d.time)/(604800000*options[1])+1);
return periode;
}
let's say my start date it 12/12/2009, then passing 12/12/2009 to this function would return 1 since it's the first "period" from this date (in week number).
What I want is to make a periodToDate function.
EDITED ANSWER BASED ON NEW INFO
Alright, this is pretty simple then. You can add values onto the date property. Try this, again, not tested.
public function addPeriodToDate(date:Date, period:int, numWeeksInPeriod:int):Date
{
var periodDate:Date = new Date(date.time);
periodDate.date += period * numWeeksInPeriod;
return periodDate;
}
END EDIT
I haven't tested this, just some quick code, but I think this should get you going in the right direction.
private function dateToPeriod(date:Date):Number
{
var d = new Date(options[0]);
var diffInMilliseconds:Number = date.time - d.time;
var diffInWeeks:Number = diffInMilliseconds / 1000 / 60 / 60 / 24 / 7;
var weeksInPeriod:Number = options[1];
var period:int = diffInWeeks / weeksInPeriod + 1;
return period;
}