MomentJS, FullCalendar time portions manipulation - javascript

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?

Related

How to round of date with momentjs?

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');

How to add timestamp in local time in momentjs

I am using moment.js and have date and time in number/timestamp. I want to convert it in my local timestamp i.e, GMT+5:30. Here is jsfiddle link.
code:-
console.log(moment(1489689000000).add(52200000).toDate())
//output: Fri Mar 17 2017 14:30:00 GMT+0530 (IST)
//expected output : Fri Mar 17 2017 20:00:00 GMT+0530 (IST)
Moment
If you want to use plain moment like in your question:
You may try to use moment.utc() if you want your date to be parsed as UTC:
console.log(moment.utc(1489689000000).add(52200000).local().toDate());
or:
console.log(moment(moment.utc(1489689000000).add(52200000).local()));
but this may not actually do what you need - depending on your system config.
Moment Timezone
If you're serious with time zones then you should be using moment-timezone:
var moment = require('moment-timezone');
var zone = moment.tz.guess();
console.log(moment.utc(1489689000000).add(52200000).tz(zone).format());
See:
http://momentjs.com/timezone/
https://www.npmjs.com/package/moment-timezone
Here some other way to achieve this. As per your GMT +5.30 timezone So, you just convert hour to milliseconds.
console.log(moment(1489689000000).add(52200000+ 19800000).toDate());
// here 5.5 hours = 19800000 miliseconds
// add + 19800000 due GMT +5.30
Ideally for your local timezone - moment object will give you the perfect date
moment().toDate() // Fri Mar 17 2017 20:18:53 GMT+0530 (IST)
but if you want for different timezone's, perfect way is to convert using moment timezone
moment().tz(timezone)toDate()
eg. moment().tz('Asia/calcutta').toDate() // Fri Mar 17 2017 20:20:49 GMT+0530 (IST)

Converting Timestamp to Date gives different timezones in the result?

I have a database where I have put down timestamps that are converted to UTC.
Now, when I am on PST, I convert 2 timestamps in Chrome, which get back to the following dates:
new Date(1446274800000)
Sat Oct 31 2015 00:00:00 GMT-0700 (PDT)
new Date(1448265600000)
Mon Nov 23 2015 00:00:00 GMT-0800 (PST)
They differ in timezone! How is this possible? I always just want to get back the PST time (or, preferably, the UTC time they were stored in).
Looks like it's daylight savings time.

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

Categories