I'm using Bootstrap DatePicker's setEndDate function (Reference).
I need to pass to it a Javascript date object. I'm trying to convert a simple year-month-day string to a date, but depending on the windows' timezone, I get different results:
In GMT -8:
> new Date('2015-01-16')
=> Thu Jan 15 2015 16:00:00 GMT-0800 (Pacific Standard Time)
In GMT +2:
> new Date('2015-01-16')
=> Fri Jan 16 2015 02:00:00 GMT+0200 (Eastern Europe Standard Time)
So what I'm left with is a different end-date depending on what timezone the user is in, even though the server side provided the same date string.
I will appreciate help on the matter.
Apparently, even though the documentation states the function's argument should be a date object, it can in fact be given a string in a "m-d-yyyy" format.
So in my case all I had to do was call the function like this:
$('#my-input').datepicker('setEndDate', '1-16-2015');
Hope this helps someone.
Related
I have a date which is the beginning of a given day in the user's browser timezone but I need to convert it to the beginning of the day in another timezone, using date-fns.
I have a date:
const date = new Date("2020-10-13"); // Tue Oct 13 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
And I need to convert it to the beginning of the day in the "America/Chicago" timezone.
const timeZone = "America/Chicago";
// Need to convert a date object
// Tue Oct 13 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
// to
// Tue Oct 13 2020 00:00:00 GMT-0500 (Central Daylight Time)
// and all I'm given is the timeZone value.
To get time zone support for date-fns, you will need the date-fns-tz add-on module.
Then you can do the following:
import { zonedTimeToUtc } from 'date-fns-tz';
const dt = zonedTimeToUtc('2020-10-13', 'America/Chicago');
The result will be a Date object that represents midnight in the given time zone.
Keep in mind that Date objects themselves are always UTC-based. Thus, you can't get a Date object that is "in" a different time zone.
Also, you should pass a string into the zonedTimeToUtc function as shown. You should not pass it to the Date object. As mentioned in comments, the ECMAScript spec says that a date-only string value should be parsed as UTC. However, there are still some implementations that don't follow the spec correctly. Thus, you should avoid parsing strings using the Date object constructor.
I wish to produce a date 30 days in the past using momentJS - it's pretty easy, I just use the following
const date30DaysPast = moment().utc().subtract(30, 'days').toDate(); // Sun Oct 29 2017 13:23:46 GMT+0100 (CET)
This is all great however I want the returned date to have no Time Zone or offset, I want the time to be GMT+0000 not as above GMT+0100 (CET) - for example:
Sun Oct 29 2017 13:23:46 GMT+0000
I wish to force this as I am using testing servers that are in different locations, and rather than take the time from the local browser I just want set a standard time. I thought using the utc method would do this for example should I write something like this in my test:
const oct4th2017 = moment.utc(new Date('October 04, 2017 11:13:00'));
the output is
Wed Oct 04 2017 09:13:00 GMT+0000
How can I remove the offset / time zone and set it to GMT on my original subtract method? I have tried wrapping in a parent utc method like so
const date30DaysPast = moment.utc(moment().utc().subtract(30, 'days').toDate());
but this doesn't work. I get the momentJS object.
Any advice could be appreciated, should my wording be bad or confusing please say so and I shall reword my question.
You already have your moment.js object without timezone. But the toDate() method creates a native Date object, which always has the local timezone (though it has various UTC methods).
I have a collection of dates and times, it is formatted like this:
01.07.2013 16:10.
I know I have to rearrange to match ISO standard, so I managed to end up with a string like this:
2013-07-01T16:10.
From this, I need to create a Date Object. After reading many questions on here about this I am a little confused about how to add a timezone to this. All of the times are in New York Cities local time, which I also want to store the dates in.
So since Javascript takes the timezone from my machine, I changed it to be the one from NYC. However, the following part confuses me the most:
> print(new Date("2013-07-01T16:10"))
Mon Jul 01 2013 16:10:00 GMT+2000 (EDT)
Question: Why does it say GMT+2000 when my local time is GMT-0400 (the one of NYC)? Is this 'correct'? If not, how is this done properly?
--Updates:
I am using mongoDB 3.4.4 and interpreter Version is MozJS-38. The systemsetup -gettimezone returns America/New_York. Running mongo-express in chrome shows: Mon Jul 01 2013 06:00:00 GMT+0200 (EDT) for ISODate("2013-07-01T04:00:00.000Z") stored, but the shell says Tue Jul 02 2013 00:00:00 GMT+2000 (EDT) for print(new Date("2013-07-01T04:00:00.000Z"));.
You didn't specify the time zone offset when creating the Date object.
> new Date("2013-07-01T16:10-04:00")
2013-07-01T20:10:00.000Z
Another JavaScript Date conundrum...
I get two completely different Date objects depending on the format I pass to the Date constructor (Date.parse() also produces different results).
Example:
new Date('04-27-2016'); => Wed Apr 27 2016 00:00:00 GMT-0500 (Central Daylight Time)
Date.parse('04-27-2016'); => 1461733200000
vs
new Date('2016-04-27'); Apr 26 2016 19:00:00 GMT-0500 (Central Daylight Time)
Date.parse('04-27-2016'); => 1461715200000
I can easily work around this situation in my code, but I would like an explanation as to why/how this is doing what it does.
The ISO format you use in the second example has UTC as its default timezone. It's described here on MDN. If you want the correct behaviour for this format, you have to add the information about timezone: new Date('2016-04-27 GMT-0500'); or you have to also specify the time: new Date('2016-04-27 00:00:00');
In my opinion it's better not to use the built-in parser and use some library instead. For example Moment.js is great.
So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)