0

I want the Date in this format(13 May 2021,11:48:25 pm). I tried the below snippet but it didn't work for me.

console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)")
.toLocaleString("en-IN", {timeStyle: "medium", dateStyle:"long", day:"2-digit", month:"long", year:"numeric"}))

Output: 13 May 2021

When I remove the day,month and year parameters I get the below output

console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)")
.toLocaleString("en-IN", {timeStyle:"medium", dateStyle:"long"}))

Output: 13/5/2021, 11:48:25 pm

How do i print the output in the desired format?

trincot
  • 317,000
  • 35
  • 244
  • 286
Snehit P
  • 13
  • 6
  • Your seconds example is giving `13 May 2021 at 11:48:25 pm` only. What is the issue ? – Nisanth Reddy May 15 '21 at 15:22
  • The desired output is 13 May 2021,11:48:25 pm and its giving 13/5/2021, 11:48:25 pm – Snehit P May 15 '21 at 15:24
  • I turned your code into snippets. When I run those, the output is not in the format as you say it is for you. – trincot May 15 '21 at 15:28
  • @trincot I just tried it once again on chrome's console. Its giving me the same output – Snehit P May 15 '21 at 15:32
  • I am getting the output that Nisanth mentions above. In Firefox, the first even produces an error. So be aware that you'll need to test more to ensure that all JavaScript engines will produce the desired outcome. – trincot May 15 '21 at 15:47
  • The format produced by *toLocaleString* is implementation dependent, so you can’t guarantee a specific format across different implementations. – RobG May 16 '21 at 02:57

4 Answers4

0

Using replace function of String is one way to do this.

console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)")
.toLocaleString("en-IN",{timeStyle:"medium",dateStyle:"long"}).replace(" at ", ","));
Nisanth Reddy
  • 5,967
  • 1
  • 11
  • 29
0

I think here is what you're looking for...

console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)")
.toLocaleString("en-IN",{timeStyle:"medium",dateStyle:"long"}).replace(' at ',','))
  • This is giving me 13/5/2021, 11:48:25 pm as output. – Snehit P May 15 '21 at 15:30
  • console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)") .toLocaleString("en-IN",{timeStyle:"medium",dateStyle:"long"}).replace(' at ',',').replace('/','').replace('/','')); –  May 15 '21 at 15:48
0

To have an exact string, without using other helper functions, we have to pass all required options to form a date string. Here's the link, where we can get a better understanding of the options we can pass in the toLocaleString method. This should give you desired output:

Intl.DateTimeFormat() constructor

console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)")
.toLocaleString("en-IN",{day:"2-digit", month:"long", year:"numeric", hour:"2-digit", minute:"2-digit", second:"2-digit"}))
Ayush Sharma
  • 2,057
  • 15
  • 25
0

I think this is your expected output -

console.log(new Date("Thu May 13 2021 23:48:25 GMT+0530 (India Standard Time)")
.toLocaleString("en-IN",{timeStyle:"medium",dateStyle:"long"}).replace('at',','))