stack flow
stack flow

Reputation: 75

Is it possible to parse a JSON date that looks like this?

I can't change the format of the date or I would, but the date is in a JSON file and looks like this.

 {
    "addlDependency": null, 
    "category": null, 
    "delay_duration": null,  
    "fromDate": "10/11/2019 07:11:17 AM", 

I want to transform the "fromDate" to 11 October is this possible with any date formats?

tried the following with no luck cause of white space.

var dateStr = JSON.parse(d.fromDate)
                        d.fromDate = new Date(dateStr)
                        console.log("date:")

Upvotes: 0

Views: 384

Answers (3)

Tejas Sarade
Tejas Sarade

Reputation: 1212

You can also use Intl.DateTimeFormat object, which is similar to date.toLocaleString(). It provides better performance if you are dealing with large number of date conversions.

var dateFormat =  { day: 'numeric', month: 'long' };
var givenDate = new Date("10/11/2019 07:11:17 AM");
var newDateString = new Intl.DateTimeFormat('default', dateFormat).format(givenDate);
console.log(newDateString);

Upvotes: 0

Developenguin
Developenguin

Reputation: 176

Try moment.js: https://momentjs.com/ You can specify the format of your string and it'll make an object for you that you can query, modify etc.

Upvotes: 0

marmeladze
marmeladze

Reputation: 6564

date = new Date("10/11/2019 07:11:17 AM")

console.log(date.toLocaleString('default', {dateStyle: 'long', month: 'long', day: '2-digit'}));

Use toLocaleString() method of Date object.

Have a look up here for more information - https://www.w3schools.com/jsref/jsref_tolocalestring.asp

Upvotes: 1

Related Questions