Flash AS3 get time in another timezone - actionscript-3

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.

Related

How to retrieve current time stamp, add X hours to it, and then store as a variable in POSTMAN?

I'm writing POSTMAN tests.
It takes a current time stamp from my account which is under UTC 00. I need to make it +11 hours and store it as a variable.
I can extract a current time frame under UTC 00 and store it as a global variable, but it misses a step for adding 11 hours to it.
var current_timestamp = new Date();
pm.globals.set("current_timestamp", current_timestamp.toISOString());
Actual stored variable:
2019-01-14T01:28:11.068Z
Expected stored variable:
2019-01-14T12:28:11.068Z
use .setHours()
var current_timestamp = new Date();
console.log(current_timestamp.toISOString())
// 2019-01-14T06:13:47.757Z
current_timestamp.setHours(current_timestamp.getHours() + 11);
console.log(current_timestamp.toISOString())
// 2019-01-14T17:13:47.757Z
pm.globals.set("current_timestamp", current_timestamp.toISOString())
Postman supports moment.js in scripts. Here is an example of how you might use it:
var moment = require('moment')
pm.globals.set('endOfDayWeekFromNowUTC', moment().endOf('day').add(1, 'weeks').utc().format('MM/DD/YYYY HH:mm:ss'))
Here is some information on how to parse dates with moment.js.

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.

Windows Phone 8 custom hourly range Live Tile update frequency

When I was searching for an answer to this, I only encountered developers looking for ways to update their apps' live tiles more and more frequently. I want the opposite, sort of.
See I'm developing a weather app, and I want it to update every hour but for a specific hourly range only. That is, I don't want the user to have the ability to update the tile once every hour because 1) people sleep and 2) the API I'm using is free only for the first 1,000 calls per day. In other words, users don't need to it update every hour and I can't afford to give them the option to anyway.
So is it possible to get, for example, the live tile to update every hour from 8am to 11pm, and to not make any calls from 12pm till 7am?
If you make the call to the API in your ScheduledAgent, simply wrap the call in an if block that checks the time. I had a similar need to update the tile once a day (it was counting down the days until Xmas).
This code is in my ScheduledAgent.cs file. It checks the date (should only trigger in December and before the 26th) and sets the countdown, then sends a toast notification only on Xmas morning. It should be a good example of how to restrict API calls to a set time of dat in your background task.
if (DateTime.Now.Month == 12 && DateTime.Now.Day < 26)
{
//number of days until the 25th
var countdown = ((new DateTime(DateTime.Now.Year, 12, 25).DayOfYear) - DateTime.Now.DayOfYear);
if (secondaryTile != null)
{
var imageString = "/Images/Tiles/" + countdown + ".png";
var newTileData = new StandardTileData
{
BackgroundImage = new Uri(imageString, UriKind.Relative)
};
secondaryTile.Update(newTileData);
}
var now = DateTime.Now;
if (now.Day == 25 && now.TimeOfDay.Hours == 9 && (now.TimeOfDay.Minutes > 14 && now.TimeOfDay.Minutes < 46))
{
var toast = new ShellToast { Title = "Xmas Countdown", Content = "Merry Xmas! Thank you for using 'Quick Xmas List' and have a safe holiday!" };
toast.Show();
}
}

Calculating runtime minus Timestamp

I have a form which activates a procedure via an "On form submit" trigger. At the end of this routine I want to insert the difference in time between the form's Timestamp and the current time at the end of the routine (the difference of which is only a matter of a few seconds).
I've tried many things so far, but the result I typically receive is NaN.
I thought that my best bet would be to construct the runtime elements (H,M,S) and similarly deconstruct the time elements from the entire Timestamp, and then perform a bit of math on that:
var rt_ts = Math.abs(run_time - ts_time);
(btw, I got that formula from somewhere on this site, but I'm obviously grasping at anything at this point. I just can't seem to find a thread where my particular issue is addressed)
I've always found that dealing with dates and time in Javascript is tricky business (ex: the quirk that "month" start at zero while "date" starts at 1. That's unnecessarily mind-bending).
Would anyone care to lead me out of my current "grasping" mindset and guide me towards something resembling a logical approach?
You can simply add this at the top of your onFormSubmit routine :
UserProperties.setProperty('start',new Date().getTime().toString())
and this at the end that will show you the duration in millisecs.
var duration = new Date().getTime()-Number(UserProperties.getProperty('start'))
EDIT following your comment :
the time stamp coming from an onFormSubmit event is the first element of the array returned by e.values see docs here
so I don't really understand what problem you have ??
something like this below should work
var duration = new Date().getTime() - new Date(e.values[0]).getTime();//in millisecs
the value being a string I pass it it 'new Date' to make it a date object again. You can easily check that using the logger like this :
Logger.log(new Date(e.values[0]));//
It will return a complete date value in the form Fri Mar 12 15:00:00 GMT+01:00 2013
But the values will most probably be the same as in my first suggestion since the TimeStamp is the moment when the function is triggered...
I have a function which can show the times in a ss with timestamps in column A. It will also add the time of the script itself to the first timestamp (in row 3) and show this in the Log.
Notice that the google spreadsheet timestamp has a resolution in seconds and the script timestamp in milliseconds. So if you only add, say, 300 milliseconds to a spreadsheet timestamp, it might not show any difference at all if posted back to a spreadsheet. The script below only takes about 40 milliseconds to run, so I have added a Utilities.sleep(0) where you can change the value 0 to above 1000 to show a difference.
function testTime(){
var start = new Date().getTime();
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
for(var i = 2; i < values.length ; i++){
Logger.log(Utilities.formatDate(new Date(values[i][0]),Session.getTimeZone(),'d MMM yy hh:mm:ss' )); // this shows the date, in my case same as the ss timestamp.
Logger.log( new Date(values[i][0]).getTime() ); // this is the date in Milliseconds after 1 Jan 1970
}
Utilities.sleep(0); //you can vary this to see the effects
var endTime = new Date();
var msCumulative = (endTime.getTime() - start);
Logger.log(msCumulative);
var msTot = (msCumulative + new Date(values[2][0]).getTime());
Logger.log('script length in milliseconds ' + msTot );
var finalTime = Utilities.formatDate(new Date(msTot), Session.getTimeZone(), 'dd/MM/yyyy hh:mm:ss');
Logger.log ( finalTime); //Note that unless you change above to Utilities.sleep(1000) or greater number , this logged date/time is going to be identical to the first timestamp since the script runs so quickly, less than 1 second.
}

Add days to Date in ActionScript

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?