How to change this 27 June 2018 - 05:25 into Y-m-d H:i:s format? I don't know how and I'm just a beginner in javascript, can someone help me?
Asked
Active
Viewed 8,550 times
0
-
3Possible duplicate of [Format JavaScript Date to yyyy-mm-dd](https://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd) – Rumit Patel May 09 '18 at 06:30
-
See this : https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – Meloman May 09 '18 at 06:34
-
@RumitPatel I visited that link awhile ago but it doesn't solve my problem – TitusMix May 09 '18 at 07:00
2 Answers
0
Using moment.js you can parse and format:
var dateStr = moment('27 June 2018 - 05:25', 'DD MMM YYYY - HH:mm').format('YYYY-MM-DD HH:mm:ss');
console.log(dateStr);
Here's a Fiddle: https://jsfiddle.net/z7jz5y8t/1/

Terry Lennox
- 29,471
- 5
- 28
- 40
-
in your fiddle, I change the date into 'inputDate' variable and the result is : Formatted date: Invalid date – TitusMix May 09 '18 at 06:45
-
1I've updated the fiddle to a new one: https://jsfiddle.net/z7jz5y8t/1/, give this a go! – Terry Lennox May 09 '18 at 06:49
0
You can set custom format like this
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return date.getFullYear() + "-"+ (date.getMonth()+1) + "-" + date.getDate() + " " + strTime;
}
var d = new Date();
var e = formatDate(d);
alert(e);
Also possible duplicate of this 'Javascript format date / time'

Abhishek
- 972
- 3
- 12
- 24