This question already has answers here:
Create a Date with a set timezone without using a string representation
(29 answers)
Closed 8 years ago.
I'm working with an API that returns time values in the following format:
2013:02:27T06:39:25
Note the lack of any identifier for timezone.
From the API Docs:
https://partner-api.groupon.com/ledger
"Transaction timestamp of the ledger entry in the affiliate's time-zone.. The format is YYYY-MM-DDThh:mm:ss.. Example 2013:02:27T06:39:25"
Apparently the API response time zone is EST (the affiliate's time-zone). What is the best way to derive a UTC timezone value from this for storage in a MongoDB database.
The format is YYYY-MM-DDThh:mm:ss.. Example 2013:02:27T06:39:25"
It looks like there is a mistake in this documentation example as it doesn't match the suggested format (2013:02:27T06:39:25 should be 2013-02-27T06:39:25).
The sample response later on that page does match the expected format:
"orderDate": "2012-11-21T04:57:03"
I would suggest using moment-timezone -- it has a moment.tz() constructor which will parse the date string and set the expected time zone:
> var moment = require('moment-timezone');
> var orderDate = moment.tz("2012-11-21T04:57:03", "America/New_York")
> orderDate.toString()
'Wed Nov 21 2012 04:57:03 GMT-0500'
> orderDate.toISOString()
'2012-11-20T17:57:03.000Z'
Related
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js
This question already has answers here:
Get the given date format (the string specifying the format) in javascript or momentjs
(2 answers)
Closed 3 years ago.
I have the following date-time string 25/10/2020 10:12:55 AM. Is there any available method that I can use to get it's format?
According docs https://momentjs.com/docs/#/parsing/creation-data/ you can use the following
moment().creationData().format
More about this feature request here https://github.com/moment/moment/issues/4595
In order to accomplish this you can add the parseFormat plugin using the following command: npm install moment-parseformat. Using this you should be able to do the following:
var format = moment.parseFormat('25/10/2020 10:12:55 AM');
format should have the date format string that you are looking for. Check out this link(https://momentjs.com/docs/#/plugins/parseformat/) for more information. I hope this helps!
You can get the format from creationData as long as you pass it something in a valid RFC2822 or ISO format:
moment('2020-10-25T10:12:55').creationData().format returns YYYY-MM-DDTHH:mm:ss
This question already has answers here:
How to ISO 8601 format a Date with Timezone Offset in JavaScript?
(21 answers)
Closed 3 years ago.
I would like to convert a Javascript Date (as in const currentDate = new Date();) to the format below:
YYYY-MM-DDTHH:MM:SS+00:00 (I know the timezone will depend on the timezone I am).
But I need the timezone in this format: +HH:00 or -HH:00. I guess the minutes for a timezone will always be zero (but maybe there are some half timezones somewhere).
Anyway, the snippet below for the current date gives me this result:
2019-11-20T18:02:57.568Z
Is there an automated way to do that conversion? Or maybe an npm package.
const currentDate = new Date();
console.log(currentDate.toISOString());
There is a NPM package which may do what you want to do in terms of converting these date-times. the link is attached below to the packages NPM page.
https://www.npmjs.com/package/date-and-time
I believe you can use the date.compile() function for your specified results.
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
Trying to get UTC day of the week for any given timestamp on any given machine (w/ their own local time) I used:
var date = new Date(timestamp).toLocaleString('en-GB', { timeZone: 'UTC' });
Once I try to convert the date string to UTC date I get Invalid Date for some dates... it all seems pretty weird.
$ node
> date = new Date('15/08/2019, 00:00:00');
Invalid Date
> date = new Date('12/08/2019, 00:00:00');
2019-12-08T00:00:00.000Z
> date = new Date('15/08/2019');
Any idea where the Invalid Date issue may come from?
By converting the timestamps to strings using the "en-GB" locale, it looks like you're getting them in DD/MM/YYYY format. But in your second example, the strings are being interpreted as "MM/DD/YYYY" in whatever your default locale is, so the first call fails because 15 isn't a valid month number.
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 4 years ago.
I have project which contain some historical data of some customers. Data contains some date column which has date like 7272017 (MMddyyyy). I've been able to convert this datetype by using c# utility by using following code.
DateTime dt=DateTime.ParseExact("12022017", "MMddyyyy",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dt);
But i want to create a utility in javascript to convert this date into ISODate. By using new Date(07272017) i am getting default date which is Thu Jan 01 1970 05:31:17 GMT+0500. How can i parse format to parse function and convert the date into other format?
This is not the same question as Converting a string to a date in JavaScript
i dont want to convert ANY string .. i want to convert a specific date format which i have done in other language but looking for solution in javascript.
[EDIT]
Moment.js is workable option but i want to convert my existing records stored in mongodb. I know i can't do it with mongo query... i am using nosqlbooster so i have access to shell which can run javascript code.. i am not sure how to link moment.js there?
If you can use moment I would advise you to do so.
var day = moment("12022017", "MMDDYYYY");
console.log(day);
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>
For loading external js into mongo shell, read this