-3

An API I use provides a date in the object like 2018-02-14T17:00:00. How can I convert this to make it say: Tuesday, February 14th 7:00 pm

I know how to use .getMonth() methods on a date object but is it possible to do something similar with a string in a date format like this in Javascript?

HellaDev
  • 408
  • 2
  • 8
  • 24

4 Answers4

4

You can use momentjs to format the date object.

console.log(new moment('2018-02-14T17:00:00').format('dddd, MMMM Do h:mm a'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Durga
  • 15,263
  • 2
  • 28
  • 52
1

You can parse the string into separated values first using String.split() method.

let rawDate = '2018-02-14T17:00:00';
let date = rawDate.split("T")[0]; //2018-02-14
let time = rawDate.split("T")[1]; //17:00:00

let year = date.split("-")[0],
  month = date.split("-")[1],
  day = date.split("-")[2];

let hr = time.split(":")[0],
  mm = time.split(":")[1],
  ss = time.split(":")[2];

Now just format these separated values using new Date(year, month, day) etc.

Abana Clara
  • 4,602
  • 3
  • 18
  • 31
0

try this

    console.log(new moment('2018-02-14T17:00:00').format('LLLL'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

JavaScript natively does not provide a way for you to stringify a date with a provided format.

To do that, you can use moment.js. Here's the specific documentation that says how to do that:

https://momentjs.com/docs/#/displaying/format/

marcofo88
  • 375
  • 2
  • 10