1

I have a date in "Mar 15, 2017 5:06:16 PM" format and i have to convert it into browser's timezone which i am doing using "Date" in javascript but the final time i am getting is in "3/15/2017, 5:06:16 PM" format i have to change it to "Mar 15, 2017 5:06:16 PM"

How this can be achieved using JavaScript?

I have tried JavaScript inbuilt functions like

(1) toISOString

(2) toLocalString

(3) toUTCString

but no luck, i feel like there is no standard js method. can anyone provide me that piece of code which can change it into the desired format ?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
whishky
  • 396
  • 3
  • 13
  • 1
    you should really consider using momentjs library, its powerful and all you want can be achieved with moment's functions... – Julo0sS Mar 15 '17 at 12:21
  • Comment in case this wouldn't work, but moment.js is a very helpful library for all things Date. – Vic Mar 15 '17 at 12:21
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – JJJ Mar 15 '17 at 12:22
  • 1
    i would suggest using moment.js. Usually when you plan on working a lot with date objects, the library offers a great variety of utilities such as custom formats, date parsing, relative time, etc – Cristi Marian Mar 15 '17 at 12:22
  • Actually this is the only use case i have with date so i dont want to use moment.js but i believe there must be something using which i can format my date. – whishky Mar 15 '17 at 12:26
  • Take in spelling JavaScript to avoid search collision with Java. – Basil Bourque Aug 10 '18 at 17:03

1 Answers1

-1

You don't have to use different methods, you can simply use string manipulation, if you want a custom date string display and don't want to load any additional libraries such as momentjs:

var months = [ 'Jan', ... ];
var parts = Date.toString() // 3/15/2017, 5:06:16 PM
            .split('/');    // [ "3", "15", "2017, 5:06:16 PM" ]

console.log( months[ parts[ 0 ] ] + " " + parts[ 1 ] + ", " + parts[ 2 ] );

This is definitely not the only solution, but whenever you want to change the display format of anything, you can always remind yourself that you can use string manipulation. A more semantically correct way to do it would be not to do any string manipulation, but by placing Date.getMonth in the wanted part of a string, for example.

If you're using ES6, you can make it very pretty:

console.log( `${monthString} ${dayNum}, ${yearNum} ${hourNum % 12}:${leftPad(minuteNum,2)}:${leftPad(secondNum,2)} ${hourNum < 12 ? 'PM' : 'AM'} ` );
towc
  • 1,983
  • 2
  • 22
  • 36
  • this will definitely decrease the code standard, thats why i decided not to use this – whishky Mar 15 '17 at 12:27
  • it's up to you to write the full semantically correct code and make it pretty. But this is the most sensible way, so long as you don't want to use a library – towc Mar 15 '17 at 12:30
  • @whishky If you don't want to use a library, then you'll have to do something similar to this answer. There is no standard function that would format the date the way you want it. – JJJ Mar 15 '17 at 12:30
  • @JJJ I guess saying it this way that "there is no other way" seems a bit rude, we can always use that "as far as i know". anyway thanks for the help – whishky Mar 15 '17 at 12:32