-1

I'm not so sure what's the proper way to insert date into the database, but I'm using new Date().

So I get date format like this when I query from the database:

2021-09-24T12:38:54.656Z

Now I realized that date format is not so user-friendly. So I'm trying to convert it if possible standard readable format like this:

Sept 25 2015, 8:00 PM

I tried using toLocaleString() to my date pulled from db but it won't work probably if I got it correctly pulled date from the db is already a string?

Is there a workaround for this so that I don't need to change how I enter date to my db?

Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
quielfala
  • 361
  • 4
  • 18
  • Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Sean Sep 24 '21 at 15:29
  • I already have the date pulled from db like this 2021-09-24T12:38:54.656Z. So it's a string that needs to be converted to date again? – quielfala Sep 24 '21 at 15:35
  • Oh so I got something like this Fri Sep 24 2021 22:22:23 GMT+0800 (China Standard Time) when I enclosed my date in new Date(). I guess I just need to work on removing the extra elements like the china standard time – quielfala Sep 24 '21 at 15:41

3 Answers3

2

const date = moment("2021-09-24T12:38:54.656Z").format('MMM D YYYY, h:mm a')
console.log(date)
<script src="https://momentjs.com/downloads/moment.min.js"></script>

use momentjs

You can find format method and apply as you desire

https://momentjs.com/

  • Thanks. Let me try this. so I guess I still need some third party package – quielfala Sep 24 '21 at 15:43
  • 1
    There are inbuild date formatting functions available for the JS Date object. Go for external libs like moment.js if those functions are not meeting your requirement. – Antajo Paulson Sep 24 '21 at 15:50
1

I think it is better to store in DB as UTC 00 value and you are doing it right now.

When you retrieve it, you will be getting a string value something like, 2021-09-24T12:38:54.656Z. On the UI you can easily convert it to date variable in JS using,

const dateVar = new Date("2021-09-24T12:38:54.656Z");
console.log(dateVar.toLocaleString());

If you want more date formatting other than the inbuild solutions like toLocaleString you can use date libraries like moment.js

Antajo Paulson
  • 555
  • 1
  • 4
  • 15
  • Oh thanks. I got it now. So if I do date inserted to database using just new Date( ) to insert current date and try to output it, I need to use new date once again to that date string and then use toLocaleString. – quielfala Sep 24 '21 at 15:49
  • 1
    In general, when you are returning data from the DB in JSON format you will be getting it as string variables. So you need to convert to JS Date variable to get Date object functions. – Antajo Paulson Sep 24 '21 at 15:53
1

You'd need to create a Date object from the timestamp string like this.

let date = new Date('2021-09-24T12:38:54.656Z');

Then you can use toLocaleString, toLocaleDateString, toDateString, or toString as you see fit.

pietnam
  • 53
  • 6
  • Yeah, I think that's all I need. I'm just confused at first because I inserted date to the db using new Date() as well. Never thought I need it again to convert the same date to something standard date format. I'm OK with the format of the toLocaleString – quielfala Sep 24 '21 at 15:53