检查不同的日期格式

Check different date formats

本文关键字:日期 格式 检查      更新时间:2023-09-26

我有函数:

sample(date){
  //operations, for example add one week (7 days) 
  return date;
}
var one = new Date('2012-07-16');
var two = new Date('07/16/2012');
var new = sample(one); // or sample(two)
var day = new.getDate();
var month = new.getMonth();
var year = new.gerYear();
alert(day + month + year);

现在我想显示这个日期,但我如何检查这个日期的格式?例如:

alert(sample(one));

应显示日期,格式为2012-07-23如果

alert(sample(one));

应显示给我2012年7月23日

但是我如何检查当前日期的格式?这可能吗?

Date对象不"记得"创建它们的格式——它们只是标准Javascript"自epoch以来的毫秒"时间值的包装器。

您需要滚动自己的"日期到字符串"函数,或者使用一个流行的现有库(例如Datejs)