1

I am new in JS and dates world. I have this string date here

12/22/2020, 1:14:09 PM

How can i convert it so at the end i would have

Tuesday, 12/22, 01:14 AM

or

Tuesday, 12/22, 01:14 PM

dynamically AM and PM

sdsd
  • 215
  • 9
  • Can you clarify what the confusion you're having is? It seems you already have the correct date format (`dddd, MM/DD, hh:mm A`), all you have to do is pass it to [`moment.js`](https://momentjs.com/). – costaparas Dec 22 '20 at 12:36
  • I don't have the correct form.The diffrecen is obviouce – sdsd Dec 22 '20 at 13:21
  • The MM/DD date format is seriously confusing, on the web you really should aim for minimum ambiguity, which can be achieved using the month name or abbreviation, e.g. MMM DD (using moment.js format tokens). – RobG Dec 22 '20 at 23:07
  • The americans want so. The clients. – sdsd Dec 23 '20 at 08:15

1 Answers1

0

The official docs here are plentiful, but here's a solution anyway:

const src = '12/22/2020, 1:14:09 PM';
const dest = moment(src, 'MM/DD/YYYY, h:mm:ss A').format('dddd, MM/DD, hh:mm A');
console.log(dest); /* Gives Tuesday, 12/22, 01:14 PM */

That should work for you, assuming you've got moment.js loaded.

costaparas
  • 4,683
  • 11
  • 14
  • 25
  • It gives me this error. https://stackoverflow.com/questions/58759601/moment-js-deprecation-warning-value-provided-is-not-in-a-recognized-rfc2822-or What is the correct format for my case ? – sdsd Dec 22 '20 at 14:13
  • You can specify the format to suppress the deprecation warning. Updated the example. – costaparas Dec 22 '20 at 14:16
  • Thank you very much. I accepted your answer and upvoted. Btw how you know what is the correct format, where i can learn this stuff ? For example you writed - 'MM-DD-YYYY, h:mm:ss A' but previous i have - 12/22/2020, 1:14:09 PM, why the format you writed is not separated with / ? Can you send me some usefull link where i can learn this stuff please... – sdsd Dec 22 '20 at 14:22
  • The official [docs](https://momentjs.com/docs/) are the best place to go for *everything* you need to know. There are also many cheat sheets online. A really useful one covering many other topics too is [here](https://devhints.io/moment). – costaparas Dec 22 '20 at 14:24
  • As for the `/` vs `-` that's a good point, I didn't realize I used `-` instead of `/` when posting it. But the date separator is irrelevant -- `moment.js` will conveniently correctly parse the date regardless of what you put there, e.g. `MM-DD-YYYY`, `MM/DD/YYYY`, `MM.DD.YYYY`, `MMDDYYYY`, etc will all work. In any case, I've updated the example to make it consistent with the input format. – costaparas Dec 22 '20 at 14:29
  • 1
    Tnank you very much – sdsd Dec 22 '20 at 14:40