获取星期的时间戳记返回NaN

时间:2019-02-02 11:15:44

标签: javascript c# timestamp nan

我想获得一周的时间戳,开始在星期一,与此类似C#脚本:

DateTime now = DateTime.UtcNow;
now = DateTime.UtcNow.StartOfWeek(DayOfWeek.Monday);
int weekday = (int)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

这将返回:1548633600

在javascript中尝试此操作时出现NaN错误:

var d = new Date();
var w = Date.UTC(thisYear, thisMonth, d.setDate(d.getDate() - (d.getDay() || 7) + 1), 0, 0, 0, 0);
var weekDay = Math.floor(w / 1000);

不确定我在做什么错。希望获得帮助:-)

2 个答案:

答案 0 :(得分:1)

var d = new Date();
var day = d.getUTCDay();

var utcTime = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() - day + 1);
var weekDay = utcTime / 1000;

console.log(weekDay);

答案 1 :(得分:0)

以这种方式尝试:这将返回星期五,将其更新为星期一

var t = new Date().getDate() + (6 - new Date().getDay() - 1) - 7 ;
var d = new Date();
d.setDate(t);
var lastFriday = d;
console.log(lastFriday);