1

I currently have all my dates being returned with the letter T in my JSON Strings. EX: 2019-02-03T06:48:07. Is there a way to change the JSON String to return as 02-03-2019 06:48:07?

I am also using javascript to load the dates currently into a data table.

4
  • You could just remove the T. What have you tried? Commented Jan 22, 2020 at 15:34
  • Your question is a little vague. I assume this JSON data comes from some sort of API. Are you maintaining this API? If so, change the data to return the date/ time in whichever format you choose. Commented Jan 22, 2020 at 15:35
  • Yes I manage the API. Ok I will look into changing that. Commented Jan 22, 2020 at 15:36
  • You may want to consider NOT changing it at the API. It depends on how reusable you want the API to be, but generally the ISO 8601 format is the preferred format for date/time information in an API. This allows the client code to determine the formatting. Should you want to change the format down the road, it is generally easier to alter and deploy the client code than changing the API. Commented Jan 22, 2020 at 15:55

2 Answers 2

2

You can convert your string into a Date object and then format it like this:

let jsonString = "2019-02-03T06:48:07";

// You should set your own timezone and options.
console.log(new Date(jsonString).toLocaleString('en-US', {hour12: false}))

For more information, visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

Sign up to request clarification or add additional context in comments.

2 Comments

that worked, is there a way to remove the comma after the date? 1/10/2020, 09:21:33
You could use replace, but it could behave unexpectedly. See this question for more info: stackoverflow.com/questions/49982572/…
1

The T is there because this is a standard ISO 8601 format. It makes it very easy to parse on the client side:

var myDate = new Date('2019-02-03T06:48:07')

With myDate you can then do whatever formatting you wish. Assuming you have another function to handle leading zeros, that could be something like:

var myFormattedDate = padLeft(myDate.getMonth() + 1, '0') + '-' + 
                      padLeft(myDate.getDay(), '0') + '-'
                      myDate.getFullYear() + ' ' +
                      // etc

Note that your timestamp lacks any timezone information. With this code it will be interpreted on the client side in whatever the local timezone of the user is. If it is UTC time, then you can correct for this by adding either 'Z' or '+00:00' onto the timestamp before parsing in the Date constructor.

If you are able to add a library to assist, this all becomes much easier with moment.js:

myFormattedDate = moment('2019-02-03T06:48:07').format('MM/DD/YYYY HH:mm:ss');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.