im trying to convert a date format into a new format in this example is d/m/Y to Ymd, this is how i do it in PHP with DateTime::createFromFormat()
is there a similer function in javascript to do this?
// 12/04/2012 work
// 12/4/2012 work
// 4/4/2012 work
// 04/4/2012 work
// 42/24/1234 not work
$date = DateTime::createFromFormat('d/m/Y', '12/04/2012');
$train_date = $date->format('Ymd'); change the format to // 20120412
in short how can i do this in javascript or nodejs?
im trying to convert a date format into a new format in this example is d/m/Y to Ymd, this is how i do it in PHP with DateTime::createFromFormat()
is there a similer function in javascript to do this?
// 12/04/2012 work
// 12/4/2012 work
// 4/4/2012 work
// 04/4/2012 work
// 42/24/1234 not work
$date = DateTime::createFromFormat('d/m/Y', '12/04/2012');
$train_date = $date->format('Ymd'); change the format to // 20120412
in short how can i do this in javascript or nodejs?
Share Improve this question edited Mar 30, 2013 at 21:23 nickhar 20.9k12 gold badges63 silver badges77 bronze badges asked Jul 1, 2012 at 16:27 user1447162user1447162 351 silver badge7 bronze badges 4- 1) Parse the ining string into a Date object. 2) Format the date as a string in the desired format. With which part are you having trouble? – Phrogz Commented Jul 1, 2012 at 16:30
- both? is there a datetime in javascript? – user1447162 Commented Jul 1, 2012 at 16:31
-
The JavaScript
Date
object is both date and time. A quick Google or searching this site will find you many solutions to parsing a date/time string in JavaScript. – Phrogz Commented Jul 1, 2012 at 16:33 - This may helps, Have a look Date Format – jittakal Commented Jul 1, 2012 at 16:35
1 Answer
Reset to default 6It's nice to use external libraries since node.js has a philosophy of minimal core library. Let's have a look at moment.js.
var moment = require('moment');
var date = moment('12/04/2012', 'DD/MM/YYYY');
var train_date = date.format('YYYYMMDD');
console.log(train_date);// 20120412