0

How do I format a date using javascript: for example, change "2018-10-25T15:00:00.000Z" to Sat, "10/25" ?

Please and thank you

Lynx
  • 23
  • 1
  • 6
  • 1
    use the Date object - it has methods to help - or, if that's too difficult, there are libraries like moment.js that make things simple – Jaromanda X Jan 15 '18 at 07:42

2 Answers2

2

Using toLocaleString you could do:

console.log(
  (new Date())
  .toLocaleString(
    window.navigator.language, 
    {month:"2-digit",day:"2-digit",weekday: 'short'}
  )
);
console.log(
  (new Date())
  .toLocaleString(
    "zh-CN", 
    {month:"2-digit",day:"2-digit",weekday: 'short'}
  )
)

Using toLacaleString will get you a string based on locale and as Rob pointed out in the comment; this may differ even in different browsers.

If you want to show the user a local string then this is good but if you want to automatically process that string it's better to use something else like milliseconds after epoch UTC.

The format you provided would suggest user only because it's unfit for automatic processing (there is no timezone or year).

Maybe you are trying to combine formatted string to show the user with something you need to process later. In that case I would advice you to use tuple (array) that contains both ms after epoch and user formatted string.

Anyway, if you want to mix up formatted for human consumption with automation then you can do the following:

var d = new Date();
["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][d.getDay()]+", "+
new String("0"+(d.getMonth()+1)).slice(-2) + "/" +
new String("0"+d.getDate()).slice(-2)
HMR
  • 37,593
  • 24
  • 91
  • 160
  • Interesting use of the `toLocaleString` method. Perhaps reframing this answer to be a function which accepts a string like the OP provided might better answer the question (Not sure if the current date is only what the question is asking for) – Chirag Ravindra Jan 15 '18 at 07:58
  • @ChiragRavindra I don't see that in the question, the question was to format a date to "WWW, MM/DD" where WWW is short day of week. That is exactly what the answer does. It is possible to parse a format string and output the date in a certain format but that wasn't in the question. – HMR Jan 15 '18 at 08:01
  • 1
    agreed. What I meant to say was that the answer works only for the current date. OP might be looking to take a string like the example provided in the question (which may be coming from a JSON API, for example). Even changing the `new Date()` to `new Date("2018-10-25T15:00:00.000Z")` might help make the usage more clear :) – Chirag Ravindra Jan 15 '18 at 08:04
  • 1
    The result of *Date.prototype.toLocaleString* is implementation dependent. Even where the components and language are specified, it doesn't guarantee a particular format. – RobG Jan 17 '18 at 01:43
  • @RobG Good comment, wanted to add this to the answer but the format OP used would suggest it's only for humans so local time would be perfect. If you want to use such a format to represent a date in your program then you are inviting lots of bugs (no time zone information and no year). Updated the answer. – HMR Jan 17 '18 at 16:31
-1

Moment.js library could probably save you a lot of time with this tedious task:

moment(new Date('2018-10-25T15:00:00.000Z')).format("dddd, MM/D")

output:

"Thursday, 10/25"

You can mess around with it by opening the JS console on their docs page, as moment is loaded on it.

Edit- as noted in comments, if this is the only task you are using it for, probably best not to use a lib and do it in plain JS!

adamz4008
  • 660
  • 4
  • 10
  • Not sure if moment is still relevant with the ECMAScript support in browsers for date formatting like [toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) – HMR Jan 15 '18 at 07:57
  • moment.js is a very useful library. However, it may be overkill to include a whole library for just this. – Chirag Ravindra Jan 15 '18 at 07:59
  • moment is perfect the only problem is that im trying to format coming in from an API. so for some reason as i do that I only get the current date – Lynx Jan 15 '18 at 15:37
  • you're getting the date from the api and passing it to moment and it's returning the current date only? that's odd, do you have a code example to reproduce? – adamz4008 Jan 15 '18 at 16:25