最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Convert ddmmyyyy or dmyyyy, etc to yyyymmdd in Javascript - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 6

It'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
发布评论

评论列表(0)

  1. 暂无评论