Why New Date() is always return null?

纵然是瞬间 提交于 2019-12-05 14:51:26

Why the new Date() does not accept if the date format having (-)?

Parsing of date strings other than the limited subset of ISO 8601 included in ECMA-262 is implementation dependent. You may get the correct result, an incorrect result or an invalid date. Therefore it is strongly recommended not to use the Date constructor or Date.parse to parse strings (they are equivalent for parsing), always manually parse strings with a library or bespoke function.

Is new Date() conversion is depend by my system date format?

No. There are a number of formats that are supported by convention (see MDN Date), however you should not rely on that as some are parsed, or parsed differently, in some browsers but not others.

You might try one of these libraries:

  1. Moment.js: parsing, formatting, arithmetic, manipulation
  2. Fecha.js: just parsing and formatting

PS

If you just want to reformat '2016-12-16 07:02:15 am' as '16/12/2016 07:02:15 am', then:

// Reformat a date like '2016-12-16 07:02:15 am' as
// '16/12/2016 07:02:15 am'
function reformatDate(s) {
  var b = s.split(/[ -]/);
  return [b[2],b[1],b[0]].join('/') + ' ' + b[3] + ' ' + b[4];
}
 
var s = '2016-12-16 07:02:15 am';
console.log(s + ' => ' + reformatDate(s))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!