前端js处理时间格式化

1,562 阅读1分钟

js获取当前时间的方法

  • var time = new Date();
  • time.getYear(); //获取当前年份
  • time.getFullYear(); //获取完整的年份(4位,1970-???)
  • time.getMonth(); //获取当前月份(0-11,0代表1月)
  • time.getDate(); //获取当前日(1-31)
  • time.getDay(); //获取当前星期X(0-6,0代表星期天)
  • time.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
  • time.getHours(); //获取当前小时数(0-23)
  • time.getMinutes(); //获取当前分钟数(0-59)
  • time.getSeconds(); //获取当前秒数(0-59)
  • time.getMilliseconds(); //获取当前毫秒数(0-999)
  • time.toLocaleDateString(); //获取当前日期
  • var mytime=time.toLocaleTimeString(); //获取当前时间
  • time.toLocaleString( ); //获取日期与时间

js获取当前时间戳

    1. var timestamp = Date.parse(new Date());//不推荐使用,因为毫秒级别的数值被转化为000 ,不准确
    1. var timestamp = (new Date()).valueOf();//获取当前毫秒的时间戳,准确!
    1. var timestamp = new Date().getTime();;//返回数值单位是毫秒

将时间戳转为正常格式

var timestamp= new Date(1472025255555)//Wed Aug 24 2016 15:54:15 GMT+0800 (中国标准时间) {}

将时间格式化

正在更新中