第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

將 c# 時(shí)間戳轉(zhuǎn)換為 javascript 時(shí)間戳

將 c# 時(shí)間戳轉(zhuǎn)換為 javascript 時(shí)間戳

C#
www說 2022-08-20 16:16:37
我有一個(gè)c#腳本,其中我為當(dāng)前年,當(dāng)前月份,當(dāng)前周和當(dāng)前日期創(chuàng)建時(shí)間戳。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;現(xiàn)在我想完成同樣的事情,但是在javascript中...我已經(jīng)得到了一年,但不知道如何得到月,周和日。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);現(xiàn)在剩下的,就是我如何才能把這一周弄對。我試過這個(gè):// 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);這將輸出一個(gè) NaN。關(guān)于我如何做到這一點(diǎn)的任何想法?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

要在一周開始時(shí)獲得星期一,您可以減去日數(shù)并添加一個(gè)。星期日為 0,因此要獲取上一個(gè)星期一,請將其日期編號更改為 7。


您錯(cuò)誤地使用了代碼,返回調(diào)整后日期的時(shí)間值,這是您想要的。您無需再執(zhí)行任何操作。但是,由于您使用的是 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 minimums

var 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 week

w.setUTCDate(w.getUTCDate() - (w.getUTCDay() || 7) + 1);


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


console.log(yearDay, monthDay, toDay, weekDay);


查看完整回答
反對 回復(fù) 2022-08-20
?
動漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

您是否在MDN上簽出了日期參考?


存在檢索此信息的方法。


// Vanilla JS

var utcDate = new Date();

console.log({

  yearDay: utcDate.getUTCFullYear(),

  monthDay: utcDate.getUTCMonth(),

  weekDay: utcDate.getUTCDay(),

  toDay: utcDate.getUTCDate(),

  hour: utcDate.getUTCHours(),

  min: utcDate.getUTCMinutes(), 

  sec: utcDate.getUTCSeconds(),

  ms: utcDate.getUTCMilliseconds()

});


// Moment

var utcMoment = moment.utc();

console.log({

  yearDay: utcMoment.year(),

  monthDay: utcMoment.month(),

  weekDay: utcMoment.isoWeekday(),

  toDay: utcMoment.date(), // Day of month or utcMoment.dayOfYear(),

  hour: utcMoment.hours(),

  min: utcMoment.minutes(), 

  sec: utcMoment.seconds(),

  ms: utcMoment.milliseconds()

});

.as-console-wrapper { top: 0; max-height: 100% !important; }

<script src="https://momentjs.com/downloads/moment.min.js"></script>

更新

如果要獲取一周中特定日期的日期,可以克隆日期,以天為單位計(jì)算差異,并從當(dāng)前日期添加/減去這些天數(shù)。


const TimeUnit = {

  YEARS : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCFullYear(date.getUTCFullYear() + amount);

      else date.setUTCFullYear(date.getUTCFullYear() + amount);

      return date;

    }

  },

  MONTHS : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCMonth(date.getUTCMonth() + amount);

      else date.setMonth(date.getMonth() + amount);

      return date;

    }

  },

  DAYS : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCDate(date.getUTCDate() + amount);

      else date.setDate(date.getDate() + amount);

      return date;

    }

  },

  HOURS : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCHours(date.getUTCHours() + amount);

      else date.setHours(date.getHours() + amount);

      return date;

    }

  },

  MINUTES : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCMinutes(date.getUTCMinutes() + amount);

      else date.setMinutes(date.getMinutes() + amount);

      return date;

    }

  },

  SECONDS : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCSeconds(date.getUTCSeconds() + amount);

      else date.setSeconds(date.getSeconds() + amount);

      return date;

    }

  },

  MILLISECONDS : {

    add : function(date, amount, utc) {

      if (utc) date.setUTCMilliseconds(date.getUTCMilliseconds() + amount);

      else date.setMilliseconds(date.getMilliseconds() + amount);

      return date;

    }

  }

};


function addTime(date, timeUnit, amount, utc) {

  return timeUnit.add(date, amount, utc);

}


function getDateForCurrentWeek(date, dayOfWeek, utc) {

  var clone = new Date(date.getTime());

  var currentDay = (utc ? clone.getUTCDay() : clone.getDay()) || 7;

  var diff = dayOfWeek - currentDay + 1;

  return addTime(clone, TimeUnit.DAYS, diff, utc);

}


function printDate(targetEl, date, utc) {

  targetEl.value = JSON.stringify(utc ? {

    year: date.getUTCFullYear(),

    month: date.getUTCMonth() + 1,

    dayOfWeek: date.getUTCDay(),

    date: date.getUTCDate(),

    hour: date.getUTCHours(),

    min: date.getUTCMinutes(), 

    sec: date.getUTCSeconds(),

    ms: date.getUTCMilliseconds()

  } : {

    year: date.getFullYear(),

    month: date.getMonth() + 1,

    dayOfWeek: date.getDay(),

    date: date.getDate(),

    hour: date.getHours(),

    min: date.getMinutes(), 

    sec: date.getSeconds(),

    ms: date.getMilliseconds()

  }, null, 2);

}


function setDayOfWeekValue(selector, date, dayOfWeek, utc) {

  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 {

  margin-bottom: 0.25em;

}

.days-of-week label {

  display: inline-block;

  font-weight: bold;

  width: 3em;

}

.days-of-week input[class^="day-"] {

  font-family: monospace;

  width: 80vw;

}

<div class="days-of-week">

  <div class="day-of-week"><label>Sun</label><input type="text" class="day-sun" /></div>

  <div class="day-of-week"><label>Mon</label><input type="text" class="day-mon" /></div>

  <div class="day-of-week"><label>Tue</label><input type="text" class="day-tue" /></div>

  <div class="day-of-week"><label>Wed</label><input type="text" class="day-wed" /></div>

  <div class="day-of-week"><label>Thu</label><input type="text" class="day-thu" /></div>

  <div class="day-of-week"><label>Fri</label><input type="text" class="day-fri" /></div>

  <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>


查看完整回答
反對 回復(fù) 2022-08-20
  • 2 回答
  • 0 關(guān)注
  • 199 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號