Date()
is returning you an integral number that represent the ms since epoch, in the timezone of your process.env
.
new Date(2017, 2, 27)
means "Give me the moment of time of 2017-02-27 00:00:00 in my current timezone". That's how this constructor works. It does not give you the start of date in UTC time and it should not because we only care about own timezone most of the time. Think about this, when I ask you what time it is now, you gonna answer me it's 9:30am (in IST, not UTC time) instead of giving me a UTC time that doesn't make sense to me.
Let's make clear few concepts:
- Date object in JS represent a moment of time. This can be simply stored as a integer in epoch time.
- There is no timezone concept in a moment of time. Regardless where you are in the world, the moment of time is all the same. (Although Einstein will not agree with this...)
Timezone represents the same moment of time in different way. Below 2 times are the exact same moment of time:
- UTC: 2017-02-26T18:30:00.000Z (the ending Z means UTC time)
- IST: 2017-02-27 00:00:00.000 GMT-5.5
Now let's explain this concept in below code:
process.env.TZ = "Asia/Kolkata";
const date = new Date(2017, 2, 27);
// Epoch: 1490553000000
console.log("Epoch:", date.getTime());
// ISO: 2017-03-26T18:30:00.000Z
console.log("ISO:", date);
// Locale: Mon Mar 27 2017 00:00:00 GMT+0530 (India Standard Time)
console.log("Locale:", date.toString());
Above 3 functions are returning the same moment in time, but in different representation.
What you asking is to get the moment in time of the start of the day in UTC+0. Using date-dns-tz is the easiest way.
import { zonedTimeToUtc } from "date-fns-tz";
// Given the date (2017-01-27) of time zone (UTC), return me a Date object of a moment of time.
// This function name is confusing. Should just be named as zonedTime.
const date = zonedTimeToUtc("2017-01-27", "UTC");
// Epoch: 1485475200000
// Again, no timezone is involved in this pure epoch time.
// Epoch converter websites will tell you this time is 2017-01-27 12:00:00 AM UTC and 2017-01-27 05:30:00 AM India Time
console.log("Epoch:", date.getTime());
// ISO: 2017-01-27T00:00:00.000Z
// This is the same moment of time represented in UTC
console.log("ISO:", date);
// Locale: Fri Jan 27 2017 05:30:00 GMT+0530 (India Standard Time)
// This is the same moment of time represented in IST
console.log("Locale:", date.toString());
Now to answer your real question. All the above is not what you are looking for.
I guess what you really want is date, but not moment of time. Timezone is irrelevant to your case. Even time is irrelevant to your case. This is a very common requirement.
Using Date object is conceptually not right and over complicated the whole thing. Date represents the exact moment of time in millisecond level (even smaller actually, can have a philosophical debate here). What you want is probably something like today is 2017-02-27.
You can consider below 2 better options:
- Store the Date (moment of time) along with the timezone. You need this if it's a global system that involves multiple time zones.
- Store it as string in YYYY-MM-DD format. I prefer this. Makes things a lot easier.
console.log(dateInCST);
? Do you get the same date as you send? – ThereaboutsDate.UTC()
. – VenduedateInCST
using theDate
object alone. TheDate
object is always UTC internally, and uses the local time zone for conversions... You can do this with moment.js, but not a rawDate
. – Idocrase