i want to figure out the time from the beginning of the day given a days milliseconds.
so say i'm given this: 1340323100024 which is like mid day of 6/21/2012. now i want the milliseconds from the beginning of the day, which would be 1340262000000 (at least i think that's what it's supposed to be.)
how do i get 1340262000000 from 1340323100024?
i tried doing
Math.floor(1340323100024/86400000) * 86400000
but that gives me 1340236800000, which if i create a date object out of it, says its the 20th.
i know i can create a date object from 1340323100024, then get the month, year, and date, to create a new object which would give me 1340262000000, but i find it ridiculous i can't figure out something so simple.
any help would be appreciated.
btw, i'm doing this in javascript if it makes any difference.
I agree with Thilo (localized to time zone), but I'd probably tackle it like this:
// Original: Thu Jun 21 2012 19:58:20 GMT-0400 (Eastern Daylight Time)
var ms = 1340323100024;
var msPerDay = 86400 * 1000;
var beginning = ms - (ms % msPerDay);
// Result: Wed Jun 20 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
Or, if you prefer:
Number.prototype.StartOfDayMilliseconds = function(){
return this - (this % (86400 * 1000));
}
var ms = 1340323100024;
alert(ms.StartOfDayMilliseconds());
EDIT
If you're particular about the timezone, you can use:
// Original: Thu Jun 21 2012 19:58:20 GMT-0400 (Eastern Daylight Time)
var ms = 1340323100024;
var msPerDay = 86400 * 1000;
var beginning = ms - (ms % msPerDay);
beginning += ((new Date).getTimezoneOffset() * 60 * 1000);
// Result: Thu Jun 21 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
Notice that the offset is now removed so the 8pm the previous day turns in to midnight of the actual day on the timestamp. You can also probably (depending on implementation) do the addition before or after you modulo for the beginning of the day--your preference.
var d = new Date();
var date = d.toISOString().split("T")[0];
var date_ms = new Date(date).getTime();
or one line answer
console.log(new Date(new Date().toISOString().split("T")[0]).getTime());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With