0

In my file HomeComponent.ts (not in template html). I create a new Date and show it in console like this:

var fecha = new Date();
console.log(fecha);

the time in my country now is 16:09 (UTC -3) but the console output shows the date in UTC:

Date 2018-12-20T19:09:32.910Z   // the time is 19:09

I need to compare and do some operations with "this new date" and other dates saved in a DB so I need the new Date to be created in my local timezone. How can I create a new Date in my local timezone?

ChrisM
  • 505
  • 6
  • 18
matQ
  • 597
  • 2
  • 13
  • 27
  • Possible duplicate of [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – nircraft Dec 20 '18 at 19:17
  • 1
    16:09-03:00 is exactly the same time as 19:09Z. The default *toString* method will use the local timezone, the console can do what it likes and may use some other method, `console.log(fecha.toString());` will give you local. – RobG Dec 20 '18 at 20:21

2 Answers2

1

How can I create a new Date in my local timezone?

Dates don't have a timezone, they are simply an offset from 1970-01-01T00:00:00Z (a time value) so are effectively always UTC. They represent a particular moment in time and can be used to generate a string representing an equivalent date and time in any timezone.

The local offset comes from the host system, it's used (if necessary) when creating a date and when working with local date and time values. There are equivalent UTC methods for doing operations that don't consider the local timezone.

The default toString method will generate a timestamp for the host timezone, toISOString will use UTC, toLocaleString can be used to generate a timestamp for any timezone. All will represent the same UTC date and time, just in different timezones.

When comparing dates, it's the UTC time value that is compared as it provides a common factor for all dates.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

the time in my country now is 16:09 (utc -3) but the console output show the date in utc

A Date or DateTime is a structure, it does not have a format. If you want to display a formatted date string using the timezone of the browser then call toLocaleString.

var fecha = new Date();
console.log("As ISO8601 in utc:", fecha);
console.log("As local:", fecha.toLocaleString());
Igor
  • 60,821
  • 10
  • 100
  • 175
  • when i convert the Data fecha.toLocaleString() it can No longer be used as a Date so I can not be able to use Date operations and comparisons on it – matQ Dec 20 '18 at 19:40
  • @matQ - see the first sentence of my response. `A Date or DateTime is a structure, it does not have a format.` You only convert to a string when you want to *display* the date to a person (like in the browser's DOM or in a csv export for example). The variable `fecha` is what you need to be using and it has the current datetime at the moment it is created. You do not need to do anything to it, it is correct as is. – Igor Dec 20 '18 at 19:42
  • @matQ - So when you call `console.log(fecha);` it has to convert to a string because of the limitations of people (we are not machines, we generally verify our environment visually) and that visual of the current date time is presented in ISO8601 notation in the UTC time zone. – Igor Dec 20 '18 at 19:44
  • in my case I receive a Date from Db and I need to compare with my localtime, so I create a new Date but it creates in UTC eg. the DB Date has time 11:00 and my localtime is 16:09 so the difference in minutes are 309 minutes but with my Date created in UTC the difference is 489 so thats worng – matQ Dec 20 '18 at 20:02
  • @matQ - chances are that the date you are receiving from the DB is not being converted correctly to the appropriate time zone. Make sure your server side code is using ISO8601 notation when sending/receiving date/times AND that the values are in UTC (using Z as a suffix). Then you have to correctly convert the server side string into a Date type, see also https://stackoverflow.com/a/5619588/1260204. – Igor Dec 20 '18 at 20:08
  • but if I save 11:00 in Db It returns me 11:00 so it is ok... the problem is that I want to create a new Date in my localtime, is there a way to do it? – matQ Dec 20 '18 at 20:13
  • @matQ - `is there a way to do it` : Yes, exactly as you have it now. It is working fine. I recommend some reading so you can better grasp how this all works together: https://www.google.com/search?q=programming+what+is+a+date and https://www.google.com/search?q=programming+what+is+utc – Igor Dec 20 '18 at 20:16