Create a Date with a set timezone without using a string representation

javascript date without timezone
javascript set timezone globally
javascript display date in different time zone
set timezone of date object js
set timezone date javascript
javascript set timezone to date object
javascript create date in another timezone
jquery change date timezone

I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date constructor that takes numbers, then I get a Date object for my current timezone:

new Date(xiYear, xiMonth, xiDate)

Give the correct date, but it thinks that date is GMT+01:00 due to daylight savings time.

The problem here is that I then pass this Date to an Ajax method and when the date is deserialised on the server it has been converted to GMT and so lost an hour which moves the day back by one. Now I could just pass the day, month, and year individually into the Ajax method, but it seems that there ought to be a better way.

The accepted answer pointed me in the right direction, however just using setUTCHours() by itself changed:

Apr 5th 00:00 GMT+01:00 


Apr 4th 23:00 GMT+01:00

I then also had to set the UTC date, month and year to end up with

Apr 5th 01:00 GMT+01:00

which is what I wanted.

using .setUTCHours() it would be possible to actually set dates in UTC-time, which would allow you to use UTC-times throughout the system.

You cannot set it using UTC in the constructor though, unless you specify a date-string.

Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.

How to create a date with a set timezone without using a string , For creating a date with a set timezone without using a string representation, Date Object method is used. More about this method can be learnt from here. It is a  Using this method, you can create a date with a set timezone. All you have to do is to use this inbuilt function by typing new Date() and then store the value in a new variable. Then display the contents of this variable using document.getElementById(“demo”).innerHTML.

var d = new Date(xiYear, xiMonth, xiDate);
d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );

This answer is tailored specifically to the original question, and will not give the answer you necessarily expect. In particular, some people will want to subtract the timezone offset instead of add it. Remember though that the whole point of this solution is to hack javascript's date object for a particular deserialization, not to be correct in all cases.

How to convert date to another timezone in JavaScript , The format() method of this object is used to return a string of the date with the How to create a date with a set timezone without using a string representation? If an offset is omitted from the string representation of a time, parsing returns a DateTime object with its Kind property set to DateTimeKind.Unspecified. If an offset is specified, parsing returns a DateTime object with its Kind property set to DateTimeKind.Local and its value adjusted to

I believe you need the createDateAsUTC function (please compare with convertDateToUTC)

function createDateAsUTC(date) {
    return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));

function convertDateToUTC(date) { 
    return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()); 

Date.parse(), The Date.parse() method parses a string representation of a date, and returns the number The following call, which does not specify a time zone will be set to  Hibernate is ignorant of timezones. Any timezone conversion should be done prior to executing the query. E.g., if your database server is set to CST, but the user is on EST, you'll need to add 1 hour to any timestamps which are the input to a query. We use a custom Hibernate date type.

Simply Set the Time Zone and Get Back According

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})

Other Time-zones are as Following

var world_timezones =

Date.prototype.getTimezoneOffset(), The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale NOT the timezone offset of the date object. 7. You still get the date for the current time zone, which agrees with the display of the timestamp. The same point in time translates to the next day in parts of Europe, when it is past 4 p.m. in California for instance. To get the date for a certain time zone, apply AT TIME ZONE first.

I don't believe this is possible - there is no ability to set the timezone on a Date object after it is created.

And in a way this makes sense - conceptually (if perhaps not in implementation); per (emphasis mine):

Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of Thursday, January 1, 1970.

Once you've constructed one it will represent a certain point in "real" time. The time zone is only relevant when you want to convert that abstract time point into a human-readable string.

Thus it makes sense you would only be able to change the actual time the Date represents in the constructor. Sadly it seems that there is no way to pass in an explicit timezone - and the constructor you are calling (arguably correctly) translates your "local" time variables into GMT when it stores them canonically - so there is no way to use the int, int, int constructor for GMT times.

On the plus side, it's trivial to just use the constructor that takes a String instead. You don't even have to convert the numeric month into a String (on Firefox at least), so I was hoping a naive implementation would work. However, after trying it out it works successfully in Firefox, Chrome, and Opera but fails in Konqueror ("Invalid Date") , Safari ("Invalid Date") and IE ("NaN"). I suppose you'd just have a lookup array to convert the month to a string, like so:

var months = [ '', 'January', 'February', ..., 'December'];

function createGMTDate(xiYear, xiMonth, xiDate) {
   return new Date(months[xiMonth] + ' ' + xiDate + ', ' + xiYear + ' 00:00:00 GMT');

Moment Timezone, If you use one of the above files, you still need moment.js , but you do not need Note that created moments have different UTC time because these moments were created in moment().tz(String) does converting to provided time zone For those, it will use Date#getTimezoneOffset and Date#toString on a handful of  It’s a lightweight numeric representation of a date. We can always create a date from a timestamp using new Date(timestamp) and convert the existing Date object to a timestamp using the date.getTime() method (see below). Dates before 01.01.1970 have negative timestamps, e.g.:

The definitive guide to JavaScript Dates, Make sure you pass a number (a string will get you an “invalid date” result pass a set of ordered values that represent each part of a date: the year, JavaScript, without any information about the timezone, will consider the  The timezone passed as 2nd argument is used as a default fall back, in case the parsed string doesn't provide TZ information.] So if you want to convert date between different timezones, you have to create two DateTimeZone objects - one for the input and one for output, like this:

Formatting Dates and Times, The DateFormat interface in ICU enables you to format a Date in milliseconds into a string representation of the date. It also parses the string back to the internal Date representation in milliseconds. You can set the time zone on the format. Create a date-time formatter using the following methods rather than constructing  date()-function automatically uses your current timezone setting but DateTime::createFromFormat (or DateTime constructor) does not (it ignores tz-parameter). You can get same results as date() by setting the timezone after object creation.

DateTime::createFromFormat - Manual, +, If this format specifier is present, trailing data in the string will not cause an error, but a time which are not specified in format will be set to the current system time. A DateTimeZone object representing the desired time zone. Be warned that DateTime object created without explicitely providing the time portion will  Text representing dates and times to convert, specified as a single character vector, a cell array of character vectors, or a string array, where each row corresponds to one date and time. datestr considers two-character years (for example, '79' ) to fall within the 100-year range centered around the current year.

  • If the accepted answer pointed you in the right direction but didn't answer your question, I would argue it shouldn't be the accepted answer. The answer should answer the question asked.
  • @alexander-abakumov can you explain your edit to the question title? You have made it no longer a question, and less specific. I'm considering rolling that back.
  • @Dan, what do you mean 'less specific'?
  • @alexander-abakumov mainly removing "JavaScript", I know we have tags on the site, but I imagine the question title is important for search engines and for people looking at search results. (Also removing "using" makes the title more ambiguous)
  • @Dan, take a look at this Meta answer. Specifically: You absolutely do NOT have to use any one of the following forms: [question title] in [tag], Stack Exchange is optimized so that tags are indexed by search engines and the system automatically prepends the most commonly used tag to the question title. If you feel that removing "using" makes it ambiguous, then let's add it back; I just tried to make the title short and concise by leaving the absolutely needed wording only.
  • The "new Date(Date.UTC(...))" syntax allows you to create a date which is equivalent to a UTC date in terms of the point in time that it represents, but it is not the same - it has a different (non UTC) time zone.
  • Keep in mind that when using "Date" the "month"-value has a range from 0-11 (not 1-12). I kept getting a timezone-offset of 2h (while it should have been 1h) and it took me hours to find out that the reason was a wrong month.
  • This answer is great. But i am using a library [datepicker ui] that is using new Date at many places. All I want is to set the UTC timezone and every date is as per new timezone. I am surprised Javascript doesn't have nothing for this.
  • @jishi—Date objects are based on a UTC time value, not local time. However, the default Date.prototype.toString method will display local time values.
  • @Anthony—"but it is not the same time" is not correct. It represents exactly the same moment in time, the only difference is the timezone offset.