1

I have a date formatted as 2018-06-13T13:28:14+0000, and I want to change it into this format 13/6/2018 1:28:14 PM.

Any suggestions will be helpful.

Thanks, in advance :)

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Naveen
  • 111
  • 1
  • 9
  • 2
    Give a look here : https://momentjs.com/ - very powerful for date/time – SG52 Jun 15 '18 at 07:00
  • 2
    HI! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. Every single possible "how do I parse/format this date" question has been asked and answered. But in the unlikely event you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Jun 15 '18 at 07:00

3 Answers3

2

Your date 2018-06-13T13:28:14+0000 is in UTC format.

One of the simplest options is to use momentjs to get desired date time format.

moment(yourDate).utc().format("DD/MM/YYYY hh:mm:ss a")

Below is code:

let date = moment("2018-06-13T13:28:14+0000").utc().format("DD/MM/YYYY hh:mm:ss a")

console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
2

the toLocaleString() function with the en-USlocale is close to what you are searching for. Would this be enough for you ? It will give you back something like :

today.toLocaleString("en-US");
// will give you => 6/15/2018, 9:03:58 AM

If you really need no comma, then use the toLocaleString() and then remove the comma like :

today.toLocaleString("en-US").replace(',','');

see : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/toLocaleString

ylerjen
  • 4,109
  • 2
  • 25
  • 43
2

To format a Date in javascript you can use Date.toLocaleDateString() method, it will give you many options to format dates.

This is a sample code snippet, you could use:

var d = new Date();
var options = {
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: true
};

console.log(d.toLocaleDateString('fr-FR', options));

Note:

Of course , you can play with options to get more possible results, you can check that in the Docs as well.

Otherwise, as suggested, you can use a JS library like Moment.js or date-format, they have better enhanced features.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • 1
    Yes agree. momentjs has just one line of code for this and it will by default show time in your local format. Check my answer please.] – Vikasdeep Singh Jun 15 '18 at 07:37
  • 1
    @VicJordan Yes, nice approach using momentjs as well, it's shorter and cleaner, but it's useful to show a ntaive JS option for doing things, as OP doesn't have momentjs tag. – cнŝdk Jun 15 '18 at 07:45