Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  198] [ 4]  / answers: 1 / hits: 28874  / 9 Years ago, mon, august 3, 2015, 12:00:00

I am formatting a Date, with not momentjs or any other library, just pure JS. And I want to know if there is a way to simplify this with ES6



let currentDate = new Date(); 

const videosInformation = {
time: currentDate.getHours() + ':' + currentDate.getMinutes(),
date: (currentDate.getMonth() + 1) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear(),
gameId: Math.floor((Math.random() * 5000) + 1)
};


I saw that in the DOM you use something like renderSomething={`something: ${someObj}`}



so you don't have to do renderSomething={something: + {someObj}}



is there something I should use to do that kind of format?


More From » date

 Answers
0

There's nothing in ES2015 that added something like strftime no. There's an ECMAScript internationalisation spec ecma-402 which enables localised time:



let [date, time] = new Date().toLocaleString('en-US').split(', ');

const videosInformation = {
time,
date,
gameId: Math.floor((Math.random() * 5000) + 1)
};


Which would give you US localized 8/4/2015 and 5:29:19 PM Or if you really want a 24 hour clock:



new Date().toLocaleString('en-US', {hour12: false})


Then you can do a substring on the time if you want to strip out the seconds.



You can read more about date and time at MDT docs.


[#65556] Friday, July 31, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aman

Total Points: 341
Total Questions: 92
Total Answers: 92

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;