Hi, I need to get the UNIX timestamp from when the day started in AS3. How can I do this???
Thanks in advance :)
Hi, I need to get the UNIX timestamp from when the day started in AS3. How can I do this???
Thanks in advance :)
Short answer: make an ActionScript Date that represents midnight for the day you care about, read its millisecond value and (if you need UNIX seconds) divide by 1000. 's math snippet (dividing by 86400 and using Math.min/answer) is not a reliable way to get a day-start timestamp. is right to point at the Date API — the missing step is forcing the time fields to midnight before reading the epoch value. (airsdk.dev)
Practical examples (local timezone):
// method A: reset an existing Date to local midnight
var nowDate:Date = new Date();
nowDate.setHours(0, 0, 0, 0); // local midnight
var unixStartLocal:Number = Math.floor(nowDate.getTime() / 1000);
// method B: construct a Date for midnight directly
var midnightFromCtor:Date = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate());
var unixStartLocal2:Number = Math.floor(midnightFromCtor.getTime() / 1000); Notes: the Date constructor parameters use local time and the month index is 0 = January. getTime() / valueOf() return milliseconds since Jan 1, 1970 (UTC), so divide by 1000 and use Math.floor if you want whole seconds. (airsdk.dev)
UTC variant and DST note:
// UTC midnight for the same calendar day
var utcMidMs:Number = Date.UTC(nowDate.getUTCFullYear(), nowDate.getUTCMonth(), nowDate.getUTCDate(), 0, 0, 0, 0);
var unixStartUTC:Number = Math.floor(utcMidMs / 1000); Use UTC when you need a timezone-independent anchor; use local-midnight when the "start of day" should follow the user's local calendar (be aware DST can shift local midnights). If you receive an external timestamp, confirm whether it's in seconds or milliseconds (timestamps > ~1e12 are almost certainly milliseconds) before creating Date objects. (airsdk.dev)
Jump to Post— JasonHippy 739Or you could use a 'Date' object... Date is a globally defined intrinsic class/type in AS3 so you don't need to import anything to be able to use it.
var today:Date = new Date(); // constructor sets a new Date object to the current system date/time by …
oh, well I found out a way.
var time:Number;
var timestamp:Number = 1283297261; // Should be unix timestamp, divide by 1000 if its a microsecond timestamp.
time = Math.min(timestamp / 86400);
time = answer*timestamp; Or you could use a 'Date' object... Date is a globally defined intrinsic class/type in AS3 so you don't need to import anything to be able to use it.
var today:Date = new Date(); // constructor sets a new Date object to the current system date/time by default
var current_time:number = today.valueOf(); // valueOf returns the number of milliseconds since midnight on january 1st 1970 (universal time!) The Date class has loads of different options to return the date in various formats.
I think valueOf() is most likely the one you're after. Otherwise take a look at the documentation for the Date class. I'm sure you'll find a function in there that will suit your needs!
Cheers for now,
Jas.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.