簡體   English   中英

根據月份名稱獲取 ISO 日期字符串

[英]Get ISO date string based on month name

我必須根據長月份名稱在數組中創建兩個 ISO 日期字符串。

Input: 'August'
Output: ['2020-08-01T00:00:00', '2020-08-31T00:00:00']

我正在考慮每個月檢查一個switch case,但我不確定如何使用此信息創建 ISO 字符串。 我可以將August08匹配並替換預定義的 ISO 字符串並根據輸入替換它,但這聽起來不是很聰明。

您可以從 toLocaleString 獲取您所在語言環境的月份名稱或對數組進行硬編碼。

然后計算本月的第一天和下個月的第 0 天

我必須將時間標准化為 15:00 以處理時區。

 const yyyy = new Date().getFullYear(); const months = Array.from(Array(12).keys()) .map(month => new Date(yyyy, month, 5, 15, 0, 0, 0) .toLocaleString('default', { month: 'long'}) ); const zeroTime = str => `${str.split("T")[0]}T00:00:00`; const getDates = month => { const monthNum = months.indexOf(month); const start = new Date(yyyy, monthNum, 1, 15, 0, 0, 0), end = new Date(yyyy, monthNum+1, 0, 15, 0, 0, 0) return [zeroTime(start.toISOString()), zeroTime(end.toISOString())]; }; console.log(getDates("August"))

有很多方法可以解決這個問題,唯一真正的問題是如何生成月份名稱列表。 以下使用 mplungian 以瀏覽器默認語言生成它們的方法,盡管我不確定這是一個好主意。

問題的另一部分是為月初和月底生成時間戳。 鑒於年和月數,這非常簡單。 您可以使用 UTC 值和toISOString ,然后修剪尾隨 Z 以獲取本地時間戳。

以下應該是有效的,因為它只生成一個日期和數組來獲取月份名稱,然后在每次調用getMonthDates 時再生成一個。 它還使用 for 循環而不是創建數組和使用數組方法進行迭代。

它還提供了單獨的函數,用於從名稱中獲取月份編號以及從月份編號和年份中獲取日期。 它們使用 ECMAScript 月份編號(0 = Jan 等),但可以輕松轉換為使用日歷月份編號(1 = Jan 等)。

 // Given year and ECMAScript month number, return an array of ISO 8601 // formatted local timestamps for first and last days of the month let getMonthDates = (monthNum = 0, year = new Date().getFullYear()) => { let date = new Date(Date.UTC(year, monthNum)); let start = date.toISOString().substring(0, 19); date.setUTCMonth(monthNum + 1, 0); return [start, date.toISOString().substring(0, 19)]; } // Given a month name, return it's ECMAScript month number // Uses host default language for month name let getMonthDatesFromName = (() => { let date = new Date(); let monthNames = (() => { date.setMonth(0, 1); for (var arr=[], i=0; i<12; i++) { arr.push(date.toLocaleString('default',{month:'long'})); date.setMonth(i+1) } return arr; })(); return (monthName, year) => { let monthNum = monthNames.indexOf(monthName); // If month name not found, return undefined return monthNum < 0? void 0 : getMonthDates(monthNum, year); } })(); /** getMonthDatesFromName examples */ // Undefined if month name not valid console.log('foo: ' + getMonthDatesFromName('foo')); // Current month name let monthName = new Date().toLocaleString('default',{month: 'long'}); console.log(monthName + ': ' + getMonthDatesFromName(monthName).toString()); // February, 2024 console.log('February, 2024: ' + getMonthDatesFromName('February', 2024).toString()); /** getMonthDates examples */ // January of current year by default console.log('Default: ' + getMonthDates().toString()); // Month of current year by default, eg for April console.log('April: ' + getMonthDates(3).toString()); // Month and year - February 2024 console.log('1, 2024: ' + getMonthDates(1, 2024).toString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM