关于时间那些小问题

获取时间的js代码

today.getFullYear();  //分别获取年月日时分秒。
today.getMonth();
today.getDate();
today.getHours();
today.getMinutes();
today.getSeconds();

有时候时间按格式不统一很麻烦,有时候显示一位数有时候显示两位数,很不舒服,就想到数字一位的时候小于9这里只用加一个判断就可以

month=month>9?month:"0"+month;

示例

var month=today.getMonth();
  month=month>9?month:"0"+month;
  var date=today.getDate();
  date=date>9?date:"0"+date;
  var hour=today.getHours();
  hour=hour>9?hour:"0"+hour;
  var min=today.getMinutes();
  min=min>9?min:"0"+min;
  var sec=today.getSeconds();
    sec=sec>9?sec:"0"+sec;
  var s=today.getFullYear()+"-"+month+"-"+date+" "+hour+":"+min+":"+sec+" ";
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容