JavaScript Create a Date in js ignoring timezone

Is it possible in js to create a date object that is not affected by timezone? For example i want to set the year "2015", the month "03" and the day "31".

var ex = new Date(2015,3,31);

If i "print" this variable now it appears like this:

Fri May 01 2015 00:00:00 GMT+0200 (ora legale Europa occidentale)

It changes to the next day. Is possible to avoid that? How can i create a Date object that is always the same in every timezone?

I need to include this behavior in an application asap and i cannot work with just Strings because we have to use a datepicker plugin (Bootstrap) which works with date objects.

Thanks in advance

Answer:1

Yes! If you create all your Date objects using the Date.UTC function, then they will all be relative to the standard UTC timezone, even if the user's browser has a different timezone set.

For example:

var ex = new Date( Date.UTC( 2015, 3, 31 ) );

This will create a Date object for 3/31/2015 UTC and be consistent across all browsers regardless of their default timezone.

Internally, the date inside ex will still be converted in the user's local timezone. So if you read values from it, say the date, it will be relative to the user's browser time.

So if you need to read the time back out in UTC, you can do something like this:

ex.getUTCYear(); // 2015
ex.getUTCMonth(); // 3
ex.getUTCDate(); // 31

This will give you the value back in UTC time.

Answer:2

I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via ...

I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via ...

  1. dynamic jquery dialog
  2. jquery dynamic dialog box example
  3. jquery dynamic dialog example
  4. dynamic jquery ui dialog
  5. jquery dynamic modal dialog
  6. create dynamic dialog jquery
  7. jquery dynamic confirm dialog
  8. dynamic content in jquery dialog
  9. dynamic buttons dialog jquery
  10. jquery dialog dynamic width
  11. jquery dialog dynamic title
  12. jquery dialog dynamic height
  13. jquery dialog dynamic button text
  14. jquery dialog dynamic text
  15. jquery dialog dynamic size
  16. jquery dialog dynamic html
  17. jquery dialog dynamic position
  18. jquery dialog dynamic div
  19. jquery dialog dynamic message

In Javascript (in Chrome devtools console panel and Node.js v0.12.5), I'm getting an incorrect answer for the product of these two large numbers: input: 41962049 * 1827116622 output: ...

In Javascript (in Chrome devtools console panel and Node.js v0.12.5), I'm getting an incorrect answer for the product of these two large numbers: input: 41962049 * 1827116622 output: ...

I'm trying to update my regular React state through Immutable, and got into some few issues. The state isn't deeply nested or it isn't nested from anything other than the state itself, such as { "...

I'm trying to update my regular React state through Immutable, and got into some few issues. The state isn't deeply nested or it isn't nested from anything other than the state itself, such as { "...

  1. updating react state
  2. updating react state array
  3. react updating state from props
  4. react updating state object
  5. react updating state in render
  6. react updating state based on props
  7. react updating state from another component
  8. react updating state directly
  9. updating react nested state properties
  10. updating react component state
  11. updating nested state react
  12. react updating parent state from child
  13. react updating parent state
  14. setstate not updating state react
  15. setstate not updating state react native
  16. updating child component state react
  17. usestate not updating state react
  18. react updating state without setstate

I got this issue working with TypeScript (for Angular2) when I try to get the id of an element. See this example: <div id="search"> <div id="field1"></div> <div id="...

I got this issue working with TypeScript (for Angular2) when I try to get the id of an element. See this example: <div id="search"> <div id="field1"></div> <div id="...

  1. typescript property getter
  2. typescript property does not exist
  3. typescript property decorator
  4. typescript property has no initializer
  5. typescript property accessor
  6. typescript property type
  7. typescript property function
  8. typescript property or signature expected
  9. typescript property initializer
  10. typescript property is missing in type
  11. typescript property is a static member of type
  12. typescript property default value
  13. typescript property shorthand
  14. typescript property exists
  15. typescript property 0 is missing in type
  16. typescript property question mark
  17. typescript property check
  18. typescript property name
  19. typescript property testing
  20. typescript property descriptor