How to round of date with momentjs? - javascript

1) My Accurate date is :Tue Apr 25 2019 00:00:00 GMT+0530
2) But in Calendar when i am selecting time i was get like this
Tue Apr 24 2019 16:56:00 GMT+0530
I was tried to fix below way
moment($scope.ProjectModel.projectStartDate).startOf('day')
But I am getting Tue Apr 24 2019 00:00:00 GMT+0530
This one is not correct, correct date: Tue Apr 25 2019 00:00:00 GMT+0530
So what i need to do to get this date ?

So, it seems like you want to set the date to the start of the next day, rather than the start of the current one. You can do it like this:
moment($scope.ProjectModel.projectStartDate).startOf('day').add(1, 'days');
Or, it seems like you want to round up if the provided date is noon or later, and down otherwise. In this case, just add 12 hours before going to the start of the day:
moment($scope.ProjectModel.projectStartDate).add(12, 'hours').startOf('day');

Related

MomentJs and dayjs both have wrong date for timezone

I'm running this code at 9:21am in London UK on Wednesday 10 August.
console.log(moment.tz(date, "America/New_York").startOf("day"), "start!")
console.log(moment.tz(date, timezone).startOf("day"), "timezone current!")
(also ran with dayjs replacing moment, results the same)
the first log returned: Tue Aug 02 2022 01:00:00 GMT+0100 (British Summer Time) {}
the second returned: Wed Aug 03 2022 01:00:00 GMT+0100
The second is correct (mostly), I'd expect 00:00:00but at least the date is right
The time currently in NY is 04:21 so why does it think the current startOf('day') date is yesterday when clearly it's still today?

MomentJS, FullCalendar time portions manipulation

I use latest fullcalendar and momentjs. My timezone is GMT+0530
Steps of my process is;
Step 1) In Fullcalendar select:func(...) I capture the date/date-range user has selected to create an event. I get the start/end dates as a moment object. If I select one day; momentjs values are;
start: Wed Nov 01 2017 00:00:00 GMT+0000
end: Thu Nov 02 2017 00:00:00 GMT+0000
Question 1 - can't I make the end date to be on the same day? Otherwise Fullcalendar creates the event for 2 days. Currently I make it to work by substracting 1 day from moment (this doesn't look right).
Step 2) Using a slider I capture the events start/end time. In slider start/end are minutes from 0 to 1440 at opposite ends. I convert the selected time range to moment object by;
moment().startOf('day').add(start, 'minutes'); //slider's left drag
moment().startOf('day').add(end, 'minutes'); //slider's right drag
Step 3) - The time selected is set to the selected date in Step 1 using;
startDate.set({ 'hour': startTime.get('hour'), 'minute': startTime.get('minute') });
endDate.set({ 'hour': endTime.get('hour'), 'minute': endTime.get('minute') });
Now, the event start/end's moment.toString() shows as an example;
Date 1: Wed Nov 01 2017 07:00:00 GMT+0000
Date 2: Wed Nov 01 2017 12:30:00 GMT+0000 //I do a date.subtract(1, 'd') to avoid fullCalendar rendering the event to an additional day. Would like to know if there is a better way
Now the event I want is properly set to Wed Nov 01 2017 from 7AM to 12.30PM
Question 2 In Fullcalendar view rendering is correct. But in the event array; each moment object's dates are correct times have got reset.
Quite frustrating... so I just set FullCalendar aside and did some tests with momentjs using the final dates that should have worked.
Date 1: Wed Nov 01 2017 07:00:00 GMT+0000
Date 2: Wed Nov 01 2017 12:30:00 GMT+0000
I convert Date 1 to a moment object by using moment(startDate, 'date');
It creates a moment object and in Console it shows the date/time as I have above. Then if I do;
start.hour() or start.minute() //= 0
Above should give me hour = 07, minute = 00 not 0 for everything?
start.toString() // = Mon Nov 20 2017 00:00:00 GMT+0530
start.get('date') // = 20!!!!
But I expect it to give me back Wed Nov 01 2017 07:00:00 GMT+0000
Question 3 What am I doing wrong?

Why is new Date() subtracting 1 day in Javascript?

I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.

Javascript huge date bug in windows... Solutions?

My dev environment is in mac.
I use bootstrap datepicker to choose dates in an application.
When a day like 27/11/2013 is selected in mac when I debug, the screen shows...
day: 1385510400
and the translation to an date object in the same debug tool is,
Wed Nov 27 2013 00:00:00 GMT+0000 (GMT)
which is correct.
All is OK, since here.
I upload the code to a windows environment and open the same page with IE8.
Start the nightmare... :)
In windows, the same day variable is shown like that...
day: 1385506800
and the translation to an date object in the same debug tool is,
Wed Nov 27 2013 00:00:00 GMT+0000 (GMT)
which, is not correct?
If we go to a external tool, the day that gave IE8 is day before, and the translation should be,
Tue, 26 Nov 2013 23:00:00 GMT.
How I can understand this?
There is no relation with the timestamp and the translation to date object, and obviously I can't find a way to make this works in both systems.
Could you wake me up from this nightmare?
Sorry for my english!
Fiddle
In JS in Chrome:
new Date(1385510400000) : Wed Nov 27 2013 01:00:00 GMT+0100
new Date(1385506800000) : Wed Nov 27 2013 00:00:00 GMT+0100
new Date("11/27/2013") : Wed Nov 27 2013 00:00:00 GMT+0100
Please note the order of the date string, it is in US order
Also note the 000 at the end; JS timestamps are in milliseconds
The same in IE8
Wed Nov 27 01:00:00 UTC+0100 2013
Wed Nov 27 00:00:00 UTC+0100 2013
Wed Nov 27 00:00:00 UTC+0100 2013
So exactly the same dates, on hour difference on the first
If you subtract the 1 hour from all dates, you get midnight on the first and 11pm on the 26th on the rest
To normalise you can do
var d = new Date(1385510400000);
d.setHours(0,0,0);
or change to UTC

Changing Date object

I'm trying to return a date function, from
Mon Jul 30 2012 10:57:56 GMT 0100 (GMT Daylight Time)
to
30 July 2012 10:57:56
Tried out most of the date functions, but still no answer/format.
Do not invent a bicycle, use library for this: http://blog.stevenlevithan.com/archives/date-time-format

Categories