I'm trying to set up the current time of a process but I just want to set up the day not the time/seconds like Tue, 28 Sep 2021.
I know 2 ways of doing dates and that would be:
new Date().toTimezoneString()
and firebase.firestore.FieldValue.serverTimestamp()
both of them includes time though.
and I know that if I set up Date()
alone it store the data as a date format instead of a string.
Extra: can it be set up in other languages as well ?
CodePudding user response:
Use Intl ( Internationalization API ) to format your dates. It's supported by all browsers and provides a comprehensive api to suit your date and time formatting needs.
Here is the doc for the method you need: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
For your use-case where you dont want to show time, you simply do not pass timeStype in the options parameter to the Intl formatter. Example would be
const date = new Date();
const formattedDate = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' }).format(date)