Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  166] [ 2]  / answers: 1 / hits: 18485  / 8 Years ago, wed, july 13, 2016, 12:00:00

My code is like this :



var createdDate = '2013-01-21 01:23:44';
createdDate = new Date(createdDate);
date = createdDate.toLocaleDateString();
time = createdDate.toLocaleTimeString().replace(/(.*)Dd+/, '$1');
console.log(date+' '+time);


The result : 1/21/2013 1:23 AM



I want the result : 2013-01-21 1:23 AM



Any solution to solve my problem?


More From » jquery

 Answers
10

There are, of course, vanilla solutions but working with date/time in JS is generally a pain. If you're going to be working with date/time in any serious capacity I would highly recommend using Moment.js's format method for its robustness and flexibility, and it should be able to do what you want.



Examples from the docs:



moment().format(); // 2014-09-08T08:02:17-05:00 (ISO 8601)
moment().format(dddd, MMMM Do YYYY, h:mm:ss a); // Sunday, February 14th 2010, 3:25:50 pm
moment().format(ddd, hA); // Sun, 3PM
moment('gibberish').format('YYYY MM DD'); // Invalid date

[#61387] Tuesday, July 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
;