将 c# 时间戳转换为 javascript 时间戳

我有一个c#脚本,其中我为当前年,当前月份,当前周和当前日期创建时间戳。


DateTime now = DateTime.UtcNow;


now = new DateTime(now.Year, 1, 1);

int yearDay = (int)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;


now = new DateTime(now.Year, now.Month, 1);

int monthDay = (int)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;


now = DateTime.UtcNow.StartOfWeek(DayOfWeek.Monday);

int weekDay = (int)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;


now = new DateTime(now.Year, now.Month, now.Day);

int toDay = (int)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

现在我想完成同样的事情,但是在javascript中...我已经得到了一年,但不知道如何得到月,周和日。


var d = new Date();


// YEAR TIMESTAMP //

var thisYear = d.getUTCFullYear();

var y = Date.UTC(thisYear, 0, 1, 0, 0, 0, 0);

var yearDay = Math.floor(y / 1000);

希望有人可以在这件事上帮助我,并提前感谢:-)


我也得到了这样的月份,但仍然需要弄清楚我如何得到今天的一天和一周。


// MONTH TIMESTAMP //

var thisMonth = d.getUTCMonth();

var m = Date.UTC(thisYear, thisMonth, 1, 0, 0, 0, 0);

var monthDay = Math.floor(m / 1000);


// C# toDay output: 1548979200

// C# weekDay output: 1548633600

在toDay中尝试了此操作,但输出与C#中的输出不同:


// DAY TIMESTAMP //

var thisDay = d.getUTCDay();

var da = Date.UTC(thisYear, thisMonth, thisDay, 0, 0, 0, 0);

var toDay = Math.floor(da / 1000);


// output: 1549324800

仍然希望得到帮助:-)


编辑 2 ***********


好吧,我终于把这一天弄对了:


// DAY TIMESTAMP //

var da = Date.UTC(thisYear, thisMonth, thisDay, 0, 0, 0, 0);

var toDay = Math.floor(da / 1000);

现在剩下的,就是我如何才能把这一周弄对。


我试过这个:


// WEEK TIMESTAMP //

var w = Date.UTC(thisYear, thisMonth, d.setDate(d.getDate() - (d.getDay() || 7) + 1), 0, 0, 0, 0);

var weekDay = Math.floor(w / 1000);

这将输出一个 NaN。关于我如何做到这一点的任何想法?


www说
浏览 217回答 2
2回答

哔哔one

要在一周开始时获得星期一,您可以减去日数并添加一个。星期日为 0,因此要获取上一个星期一,请将其日期编号更改为 7。您错误地使用了代码,返回调整后日期的时间值,这是您想要的。您无需再执行任何操作。但是,由于您使用的是 UTC 值,因此它位于您的上下文中。d.setDate(...)我已更改变量名称,以包含UTC作为其值的提示。此外,“Date”方法返回月份中的日号,“Day”方法返回一周中的日号。var d = new Date();var utcYear = d.getUTCFullYear();// Only year and month need to be set, missing values will default to minimumsvar yearDay = Math.floor(Date.UTC(utcYear, 0) / 1000);// MONTH TIMESTAMP //var utcMonth = d.getUTCMonth();var monthDay = Math.floor(Date.UTC(utcYear, utcMonth) / 1000);// DAY TIMESTAMP // - changed "Day" to "Date"var utcDate = d.getUTCDate();var toDay = Math.floor(Date.UTC(utcYear, utcMonth, utcDate) / 1000);// WEEK TIMESTAMP //var w = new Date(Date.UTC(utcYear, utcMonth, utcDate));// Set w to Monday at start of weekw.setUTCDate(w.getUTCDate() - (w.getUTCDay() || 7) + 1);var weekDay = Math.floor(w / 1000);console.log(yearDay, monthDay, toDay, weekDay);

动漫人物

您是否在MDN上签出了日期参考?存在检索此信息的方法。// Vanilla JSvar utcDate = new Date();console.log({&nbsp; yearDay: utcDate.getUTCFullYear(),&nbsp; monthDay: utcDate.getUTCMonth(),&nbsp; weekDay: utcDate.getUTCDay(),&nbsp; toDay: utcDate.getUTCDate(),&nbsp; hour: utcDate.getUTCHours(),&nbsp; min: utcDate.getUTCMinutes(),&nbsp;&nbsp; sec: utcDate.getUTCSeconds(),&nbsp; ms: utcDate.getUTCMilliseconds()});// Momentvar utcMoment = moment.utc();console.log({&nbsp; yearDay: utcMoment.year(),&nbsp; monthDay: utcMoment.month(),&nbsp; weekDay: utcMoment.isoWeekday(),&nbsp; toDay: utcMoment.date(), // Day of month or utcMoment.dayOfYear(),&nbsp; hour: utcMoment.hours(),&nbsp; min: utcMoment.minutes(),&nbsp;&nbsp; sec: utcMoment.seconds(),&nbsp; ms: utcMoment.milliseconds()});.as-console-wrapper { top: 0; max-height: 100% !important; }<script src="https://momentjs.com/downloads/moment.min.js"></script>更新如果要获取一周中特定日期的日期,可以克隆日期,以天为单位计算差异,并从当前日期添加/减去这些天数。const TimeUnit = {&nbsp; YEARS : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCFullYear(date.getUTCFullYear() + amount);&nbsp; &nbsp; &nbsp; else date.setUTCFullYear(date.getUTCFullYear() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; },&nbsp; MONTHS : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCMonth(date.getUTCMonth() + amount);&nbsp; &nbsp; &nbsp; else date.setMonth(date.getMonth() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; },&nbsp; DAYS : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCDate(date.getUTCDate() + amount);&nbsp; &nbsp; &nbsp; else date.setDate(date.getDate() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; },&nbsp; HOURS : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCHours(date.getUTCHours() + amount);&nbsp; &nbsp; &nbsp; else date.setHours(date.getHours() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; },&nbsp; MINUTES : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCMinutes(date.getUTCMinutes() + amount);&nbsp; &nbsp; &nbsp; else date.setMinutes(date.getMinutes() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; },&nbsp; SECONDS : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCSeconds(date.getUTCSeconds() + amount);&nbsp; &nbsp; &nbsp; else date.setSeconds(date.getSeconds() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; },&nbsp; MILLISECONDS : {&nbsp; &nbsp; add : function(date, amount, utc) {&nbsp; &nbsp; &nbsp; if (utc) date.setUTCMilliseconds(date.getUTCMilliseconds() + amount);&nbsp; &nbsp; &nbsp; else date.setMilliseconds(date.getMilliseconds() + amount);&nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; }&nbsp; }};function addTime(date, timeUnit, amount, utc) {&nbsp; return timeUnit.add(date, amount, utc);}function getDateForCurrentWeek(date, dayOfWeek, utc) {&nbsp; var clone = new Date(date.getTime());&nbsp; var currentDay = (utc ? clone.getUTCDay() : clone.getDay()) || 7;&nbsp; var diff = dayOfWeek - currentDay + 1;&nbsp; return addTime(clone, TimeUnit.DAYS, diff, utc);}function printDate(targetEl, date, utc) {&nbsp; targetEl.value = JSON.stringify(utc ? {&nbsp; &nbsp; year: date.getUTCFullYear(),&nbsp; &nbsp; month: date.getUTCMonth() + 1,&nbsp; &nbsp; dayOfWeek: date.getUTCDay(),&nbsp; &nbsp; date: date.getUTCDate(),&nbsp; &nbsp; hour: date.getUTCHours(),&nbsp; &nbsp; min: date.getUTCMinutes(),&nbsp;&nbsp; &nbsp; sec: date.getUTCSeconds(),&nbsp; &nbsp; ms: date.getUTCMilliseconds()&nbsp; } : {&nbsp; &nbsp; year: date.getFullYear(),&nbsp; &nbsp; month: date.getMonth() + 1,&nbsp; &nbsp; dayOfWeek: date.getDay(),&nbsp; &nbsp; date: date.getDate(),&nbsp; &nbsp; hour: date.getHours(),&nbsp; &nbsp; min: date.getMinutes(),&nbsp;&nbsp; &nbsp; sec: date.getSeconds(),&nbsp; &nbsp; ms: date.getMilliseconds()&nbsp; }, null, 2);}function setDayOfWeekValue(selector, date, dayOfWeek, utc) {&nbsp; document.querySelector(selector).value = getDateForCurrentWeek(date, dayOfWeek, utc);}var utcDate = new Date();setDayOfWeekValue('.day-sun', utcDate, 0, true);setDayOfWeekValue('.day-mon', utcDate, 1, true);setDayOfWeekValue('.day-tue', utcDate, 2, true);setDayOfWeekValue('.day-wed', utcDate, 3, true);setDayOfWeekValue('.day-thu', utcDate, 4, true);setDayOfWeekValue('.day-fri', utcDate, 5, true);setDayOfWeekValue('.day-sat', utcDate, 6, true);// Test adding 1 unit to each date portion.printDate(document.getElementById('result-1'), utcDate, true);utcDate = addTime(utcDate, TimeUnit.YEARS, 1, true);utcDate = addTime(utcDate, TimeUnit.MONTHS, 1, true);utcDate = addTime(utcDate, TimeUnit.DAYS, 1, true);utcDate = addTime(utcDate, TimeUnit.HOURS, 1, true);utcDate = addTime(utcDate, TimeUnit.MINUTES, 1, true);utcDate = addTime(utcDate, TimeUnit.SECONDS, 1, true);utcDate = addTime(utcDate, TimeUnit.MILLISECONDS, 1, true);printDate(document.getElementById('result-2'), utcDate, true);.days-of-week .day-of-week {&nbsp; margin-bottom: 0.25em;}.days-of-week label {&nbsp; display: inline-block;&nbsp; font-weight: bold;&nbsp; width: 3em;}.days-of-week input[class^="day-"] {&nbsp; font-family: monospace;&nbsp; width: 80vw;}<div class="days-of-week">&nbsp; <div class="day-of-week"><label>Sun</label><input type="text" class="day-sun" /></div>&nbsp; <div class="day-of-week"><label>Mon</label><input type="text" class="day-mon" /></div>&nbsp; <div class="day-of-week"><label>Tue</label><input type="text" class="day-tue" /></div>&nbsp; <div class="day-of-week"><label>Wed</label><input type="text" class="day-wed" /></div>&nbsp; <div class="day-of-week"><label>Thu</label><input type="text" class="day-thu" /></div>&nbsp; <div class="day-of-week"><label>Fri</label><input type="text" class="day-fri" /></div>&nbsp; <div class="day-of-week"><label>Sat</label><input type="text" class="day-sat" /></div></div><textarea id="result-1" rows="12" cols="32"></textarea><textarea id="result-2" rows="12" cols="32"></textarea>
打开App,查看更多内容
随时随地看视频慕课网APP