小程序有没有对时间的方法
发布于 6 年前 作者 bmeng 9475 次浏览 来自 问答

我想求当前时间 一个星期后的年月日

//当前时间

function getYMDTime(date) {

  var year = date.getFullYear()

  var month = date.getMonth() + 1

  var day = date.getDate()

  return year + “-” + month + “-” + day

}

1 回复

var timestamp = Date.parse(new Date());

console.log(timestamp);

var nextweek = timestamp + 24 * 60 * 60 * 7*1000;

console.log(nextweek);

var date = new Date(nextweek);//时间戳为10位需*1000,时间戳为13位的话不需乘1000

var Y = date.getFullYear() + ‘-’;

var  M = (date.getMonth() + 1 < 10 ? ‘0’ + (date.getMonth() + 1) : date.getMonth() + 1) + ‘-’;

var  D = date.getDate() + ’ ';

var h = date.getHours() + ‘:’;

var  m = date.getMinutes() + ‘:’;

var s = date.getSeconds();

var res =  Y + M + D + h + m + s;

console.log(res);

回到顶部