var dts = "2019-05-26" // this value came from browser query like "d=1&date=2019-05-26"
var date = new Date(dts)
console.log(JSON.stringify(date))
which prints:
#=> "2019-05-25T19:00:00.0000Z"
Problem
I get this date from user input. Format only contains year, month and day. Problem happens when user browser's timezone applied on parsing. Sometimes, I get correct date in a day but sometimes i get one day before. This causes wrong database querying.
How can I convert this Date object to UTC? Because I need it as a Date object not as a string.
Is there any library that can help me parsing dates at UTC and get back as Date Object?
Use Moment UTC to normalize the time
Related
I have a very simple SQL string that brings a Date field from sql:
SELECT dbo.table.effDate from table where.....
I need to convert that date to a date variable at Node so I can perform a calculation. This date is always the first of a month, so my results at SQL are
2023-01-01
2022-05-01
2022-08-01
etc.
So, when I try to convert the field to a node date type, in these different ways, I always get the previous date of the one at the field (for the example above)
2022-12-31
2022-04-30
2022-07-31
Im using:
let effDate = moment(response[i].effDate, 'YYYY-MM-DD').format('YYYY-MM-DD')
Then
let effDate = new Date(response[i].effDate)
even this is bringing me a previous date
let effDate = response[I].effDate.toString()
and this:
let effDate = new Date(response[I].effDate).toLocaleDateString('en-US')
I could fix this by adding one day to whatever SQL brings me back, but its not the point, I want to do it right.
I also tried using getTimezoneOffset
effDate = new Date(response[i].effdate.getTime() - response[i].effdate.getTimezoneOffset() * 60000)
But I always get the previous date.
What confuses me is that javascript UTC zones should have nothing to do when it comes to the value returned by SQL:
Again, I could just add a day but that wont be right, and I would like to know why this is happening (even using toString()).
Thanks.
Basically Date treats strings as being the UTC representation of that moment in time (because its the standard format to save a date in a database). Running the following line should give you a better understanding of this:
console.log(new Date('2023-01-01').toString())
I get:
Sat Dec 31 2022 16:00:00 GMT-0800 (Pacific Standard Time)
Either allow moment.js to interpret the string directly, or use the .utc() method.
// moment from Date object is still UTC, outputs local
console.log('A:', moment(new Date('2023-01-01')).format('YYYY-MM-DD'))
// moment parses string assumming local
console.log('B:', moment('2023-01-01').format('YYYY-MM-DD'))
// use moment.utc() method to keep output UTC
console.log('C:', moment(new Date('2023-01-01')).utc().format('YYYY-MM-DD'))
console.log('D:', moment.utc(new Date('2023-01-01')).format('YYYY-MM-DD'))
console.log('E:', moment.utc('2023-01-01').format('YYYY-MM-DD'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
A web request gives me '2022-03-01'.
I know that is and always will mean UTC 2022-03-01 at midnight exactly.
I need to copy that value onto another CRM date field on the Form
I tried:
var passDateToLib = Date('2022-03-01')
formContext.getAttribute("new_otherdatefield").setValue(passDateToLib)
That new_otherdatefield field is also configured to be a UTC Date only field.
But what ends up shown in the field, is 1 date BEFORE '2022-03-01'. So I suspect it's ignoring the timezone aspect of the Date...
toISOString() gives the time in ISO format. To get the date only as you mentioned we can use substring method to remove the time. This will basically give the date based on GMT and not for your Local TimeZone.
new Date().toISOString().substring(0,10);
I have two dates on the client
openTime: 2020-01-01 00:00:000 (GMT+2)
closeTime: 2020-01-01 06:00:000 (GMT+2)
Before sending it to the server I convert them to ISO
openTime.toISOString() // 2019-12-31T22:00:00.000Z
closeTime.toISOString() // 2020-01-01T04:00:00.000Z
Server receives these dates and applies validation logic
isSameDay(openTime, closeTime) // false
The comparison result is false. It happens because of timezones. Whenever openTime with such time converts to UTC the date "jumps" into the different date.
Before converting to ISO
openTime: 2020-01-01 00:00:000
After converting to ISO
openTime: 2019-12-31T22:00:00.000Z
How to solve this issue?
Is it okay to send client timeZoneOffset beside openTime & closeTime values.
..or, send openTime & closeTime with custom format which adds timezone to the ISO string?
FWIW, I'd go with your concept of formatting the date with the timezone in it. Make the timezone part +HH:MM or -HH:MM (four digits) and the JavaScript Date object will be able to parse it correctly. (That's part of the ISO 8601 subset that JavaScript supports.) So for instance, 2021-08-02T16:03:26.499+02:00.
Then, in your server-side, to see whether the open and close are on the same day, you can just use string comparison: open.substring(0, 10) === close.substring(0, 10).
I am trying to get specific format of datetime with time zone
i am getting string of time format which is shown below
var dateTime = "2020-06-01T01:50:57.000Z CDT"
I need to convert the format in to
const offsetTime = moment(date).add("-0.00", 'hours')
const formatedDate = moment(offsetTime, 'h:mm:ss A')
.utc()
.format('h:mm A')//(1:50 AM)
Required output
(1:50 AM CDT)
Do i need to split the string and get the format or do we have any method to convert it to this format in momentjs
In simple way to say
YYYY-MM-DDTHH:mm:ss.SSS[Z] z To hh:mm A z //format
and if the string contains only 2 character like "CT" instead of CDT how to capture that.
You can zz to get timezone in output. For ex:
moment()..format('h:mm A zz')
More documentation here momentJS
Use the moment-timezone to achieve this. Use the moment constructor to specify the input format, then specifying the required timezone. Finally use moment's format to get the required format
var dateTime = "2020-06-01T01:50:57.000Z CDT";
var timezone = "America/Chicago";
console.log(
moment(dateTime, "YYYY-MM-DD hh:mm:ss zz")
.tz(timezone)
.format("h:mm A zz")
);
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script>
Your date string is in ISO format with the 'Z' after seconds indicating that it is in UTC time. I am assuming that the 'CDT' is placed in the string in order to indicate which time zone this should be converted to. If you have control over how this string is represented then I recommend changing it so that you indicate the desired timezone elsewhere and simply store the date in UTC format. This way you can initialize a date or moment object with the ISO string as follows:
var date = moment("2020-06-01T01:50:57.000Z")
It is inconvenient the way it is currently since you cannot initialize it this way:
var date = moment("2020-06-01T01:50:57.000Z CDT")
The only option for handling the date in its current form is to parse it. You can do that like this:
var dateTime = "2020-06-01T01:50:57.000Z CDT"
var trimmed = dateTime.trim() // remove leading and trailing whitespace
var isoString = trimmed.substr(0, trimmed.indexOf(' '))
Which will produce the following string
2020-06-01T01:50:57.000Z
You can use that string I called "isoString" to initialize a date or moment object. The next obstacle is to handle converting that UTC string to a certain timezone (in this case CDT). It is simple if you want to convert the UTC date to the current users timezone since that will happen automatically when you initialize the moment or date object with the ISO date string. Otherwise, you need some way to get the timezone from 'CDT' into the format moment wants which was shown by #vjr12 ("America/Chicago"). The only way to do this is to either store that with the date string or create a mapping. It is much easier to convert from "America/Chicago" to "CDT" than it is to convert from "CDT" to "America/Chicago". Your only option with the current form is to create your own mapping from "CDT" to "America/Chicago". You could do something like:
let tzMap = new Map()
tzMap.set('CDT','America/Chicago')
// Set the rest of your timezones
You would need to do that for all timezones and then you could use the timezone parsed from your date string like this:
var tzAbbr = trimmed.substr(trimmed.indexOf(' ') + 1)
which will grab the "CDT" or "CT" for that matter. Then you could use your mapping like this:
var timezone = tzMap.get(tzAbbr)
timezone will be "America/Chicago" in this case and then you can use #vjr12 solution from here to get the form you want.
Note
I highly recommend that (if you are able) to change the current format of the datestring that you are using. The purpose of using UTC time is to be timezone agnostic so it does not make sense to store the timezone with the UTC string. If you want to preserve the timezone then you would be better off using a format which already embeds the timezone.
Hi im using moment js to convert this string 20:00 I tried:
var a = moment("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00+01:00
the problem when I store in mongodb it become
2016-09-10T19:00:00.000Z
I want to store 2016-09-10T20:00:00.000Z
anyway can explain why please ?
When you say that you want to store 2016-09-10T20:00:00.000Z what you are saying is that you want to assume that your date and time is UTC.
To assume that the date you are parsing is a UTC value, use moment.utc
var a = moment.utc("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00Z
Note that when you parse a time without a date, moment assumes the current date. This may not be the behavior that you want.
I'm also not sure if you want a UTC date (which is what you are saying), or a local date without an offset indicator. If you want a local date without an offset indicator, simply use a format without an offset:
moment.utc("20:00", "HH:mm").format('YYYY-MM-DDTHH:mm:ss.SSS')
"2016-09-08T20:00:00.000"
If you are dealing with local dates that do not have a time zone association, I recommend using moment.utc to parse, as this will ensure that the time does not get shifted to account for DST in the current time zone.
For more information about how to parse dates into the time zone or offset that you would like in moment, see my blog post on the subject.
This it how it should look:
var a = moment("20:00", "HH:mm")
console.log(a.utcOffset('+0000').format())
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Doe, the problem is that you are using timezones when you create the date.
MomentJS uses your current timezone automatically.
Mongo however saves the time as it would be in another timezone.
Therefore, if you want the two strings to format the same way, you need to set the timezone.