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
Related
This question already has answers here:
Convert .NET date format into JavaScript date
(1 answer)
Parsing a Auto-Generated .NET Date Object with Javascript/JQuery
(2 answers)
Closed 2 years ago.
I'm attempting to convert a Date that is returning from the database as /Date(1570474808430)/. I'd like to convert it as a standard date time result such as it's appearing in the database as : 2020-08-04 11:08:22.630. Is this something that is possible? I'm not entirely sure why it appears as date time in the database but is returning as /Date(1570474808430)/ on the front end.
I've attempted to approach this with the following code:
let oldDate = /Date(1570474808430)/
let updatedDate = new Date(oldDate);
console.log(updatedDate)
My expected result is to convert /Date(1570474808430)/ to a date time: 2020-08-04 11:08:22.630
Your snippet, perhaps unknown to you, actually tries to pass a regular expression literal to the Date constructor. What comes back from your API is actually a string - of the form /Date(xxx)/ where xxx appears to be a Unix timestamp. In order to translate this to a Javascript date object, we need to parse the timestamp out of that - assuming the data always has this format - and ironically, the simplest way to do that is probably a regular expression:
const oldDate = "/Date(1570474808430)/";
const timeStamp = Number(oldDate.match(/\/Date\((\d+)\)\//)[1]);
const updatedDate = new Date(timeStamp);
console.log(updatedDate)
This works if you can guarantee your data will be in this form - but frankly it is not good code, and will certainly lead to problems if your data isn't always in this format. The best thing you could do, if it's possible, would be to update your data so that it holds a sensible date-string in a standard format. Or failing that, at least a number representing the Unix timestamp, without the /Date(...)/ cruft.
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:
Convert time interval given in seconds into more human readable form
(24 answers)
Closed 3 years ago.
I am returning some dates directly (unmodified) from the a database query to the front-end:
1540906457020
1540920856937
1540920856970
What is this dateformat called and how do I convert it to a human readable date, such as "03/21/2019" (or some variant with timestamp appended)?
It's based on the Unix timestamp, but it's counting milliseconds rather than seconds. (ES2015 calls this the "Time Value".) The Date object in Javascript uses this value underneath the surface. If you use the integer value as a parameter in the Date constructor, you'll get a Date object which should be handled quite well by most browsers.
const happyDateObject = new Date(1540920856937);
If you want a bit more control over what's going on, or want some more utilities that help you customize what the date looks like and how to manipulate it, I'd recommend the moment.js library. It's widely used because it's so useful. Since it's really just a wrapper for the standard Javascript Date object, moment objects convert quite easily to Date objects (when you need to do so). You'd construct the value in a similar way:
const happyMoment = moment(1540920856937)
This is a Unix timestamp (since Jan 01 1970), in milliseconds
You can convert it using e.g. this link here: https://www.epochconverter.com/
There are plenty of topics on how to convert timestamps to strings.
Start by using
new Date(1540920856937)
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'
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How can I convert datetime microformat to local time in javascript?
Im writing up an ajax application where i have to interpret this date "2009-09-16T11:10:00" and output another string to something more readable.
That's the ISO 8601 date format. There's an example here. If that doesn't suit your needs then a quick google search should help.
No, there isn't a built-in function for doing that. You'd have to parse it yourself. Maybe something like this:
var s = "2009-09-16T11:10:00";
var tokens = s.split(/[\-T:]/);
var date = new Date(tokens[0], tokens[1] - 1, tokens[2],
tokens[3], tokens[4], tokens[5], 0);
Then access the date string with:
alert(date.toString());
Try this js library:
http://www.datejs.com
Pretty good and recognizes different date formats. You can also test your date right on the front page.