I get time in milliseconds from the server. I convert it to Date and get - Mon Jul 22 2013 11:16:01 GMT+0200 (W. Europe Daylight Time) as the date in the record.
I want to separate out data of Monday, Tuesday etc into arrays. I am thinking of converting this date to Mon Jul 22 2013 23:59:59 GMT+0200 (W. Europe Daylight Time) and then filter out the records.
How can i change the date to the required end of the day time? or is there an easier way to do this ?
I get time in milliseconds from the server. I convert it to Date and get - Mon Jul 22 2013 11:16:01 GMT+0200 (W. Europe Daylight Time) as the date in the record.
I want to separate out data of Monday, Tuesday etc into arrays. I am thinking of converting this date to Mon Jul 22 2013 23:59:59 GMT+0200 (W. Europe Daylight Time) and then filter out the records.
How can i change the date to the required end of the day time? or is there an easier way to do this ?
Share Improve this question asked Jul 30, 2013 at 16:06 DanDan 8292 gold badges15 silver badges29 bronze badges 3- I don't quite understand what it is you're after. Could you show an example of the result you're looking for? – JJJ Commented Jul 30, 2013 at 16:08
- 1 Usually people just zero out the hour, minute, sec and ms and use the start of the day. That works pretty well unless you have to compare millisecond times to check for events on that day and even that isn't hard. – Lee Meador Commented Jul 30, 2013 at 16:08
- Which time zone do you want to use: client's TZ or some other one (like GMT)? – SheetJS Commented Jul 30, 2013 at 16:13
5 Answers
Reset to default 19You could always construct a new DateTime object just using the year, month and day properties from the existing date, like so:
var actualDate = new Date(); // 2013-07-30 17:11:00
var endOfDayDate = new Date(actualDate.getFullYear()
,actualDate.getMonth()
,actualDate.getDate()
,23,59,59); // 2013-07-30 23:59:59
For future visitors, just use
const start = new Date();
const end = new Date();
start.setHours(0, 0, 0, 0);
end.setHours(23, 59, 59, 999);
Using http://momentjs.com:
var now = new Date().getTime();
var endOfDay = moment(now).endOf("day").toDate(); // Wed Jan 20 2016 23:59:59 GMT-0800 (PST)
var actualDate = new Date()
var eodDate = new Date(Math.floor(actualDate.getTime()/86400000+1)*86400000 + actualDate .getTimezoneOffset()*60000 - 1000)
where 86400000 are total milliseconds in a day
If two Date Objects are on the same day then they have the same Date String:
new Date('1374488161000').toDateString()
=> "Tue Jul 30 2013"
new Date('13744917610403').toDateString()
=> "Tue Jul 30 2013"
Although a rather naive method of comparing days, it's probably the simplest comparison.