JavaScript之通過年月獲取月份的天數(shù)、日期格式化、時間、補零、Date、toLocaleString、Intl、DateTimeFormat、format(問題總結)
版本一
function createDays(year = 2024, month = 2) { let sDate = new Date(year, month - 1, 1), eDate = new Date(year, month, 0), days = []; sDate = sDate.getDate(); eDate = eDate.getDate(); for (let i = sDate; i <= eDate; i++) { let ds = new Date(year, month - 1, i); ds = ds.toLocaleString("en-US", { timeZone: "Asia/Shanghai" }); ds = ds.split(',')[0]; ds = ds.split('/'); [ds[0], ds[1], ds[2]] = [ds[2], ('0' + ds[0]).slice(-2), ('0' + ds[1]).slice(-2)]; ds = ds.toString(); ds = ds.replace(/,/g, '-'); days.push(ds); } return days; } console.log(createDays(2023, 1)); console.log(createDays(2023, 2)); console.log(createDays());
版本二
function createDays(year = 2024, month = 2) { let sDate = new Date(year, month - 1, 1), eDate = new Date(year, month, 0), formatter = new Intl.DateTimeFormat('default', { timeZone: 'Asia/Shanghai', year: 'numeric', month: 'numeric', day: 'numeric' }), zeroFill = (val) => ('00' + val).slice(-2), days = []; sDate = sDate.getDate(); eDate = eDate.getDate(); for (let i = sDate; i <= eDate; i++) { let ds = new Date(year, month - 1, i); ds = formatter.format(ds); ds = ds.split('/'); ds[1] = zeroFill(ds[1]); ds[2] = zeroFill(ds[2]); ds = ds.toString(); ds = ds.replace(/,/g, '-'); days.push(ds); } return days; } console.log(createDays(2023, 1)); console.log(createDays(2023, 2)); console.log(createDays());
版本三
function createDays(year = 2024, month = 2) { let sDate = new Date(year, month - 1, 1), eDate = new Date(year, month, 0), zeroFill = (val) => ('00' + val).slice(-2), days = []; sDate = sDate.getDate(); eDate = eDate.getDate(); for (let i = sDate; i <= eDate; i++) { let ds = new Date(year, month - 1, i); ds = ds.toLocaleString( "en-US", { timeZone: "Asia/Shanghai", year: 'numeric', month: 'numeric', day: 'numeric' } ); ds = ds.split('/'); [ds[0], ds[1], ds[2]] = [ds[2], zeroFill(ds[0]), zeroFill(ds[1])]; ds = ds.toString(); ds = ds.replace(/,/g, '-'); days.push(ds); } return days; } console.log(createDays(2023, 1)); console.log(createDays(2023, 2)); console.log(createDays());
效果圖
解析
Date
在JavaScript中,Date對象用于處理日期和時間。Date構造函數(shù)可以接受多種參數(shù),這些參數(shù)用于初始化Date對象,從而表示特定的日期和時間。
無參數(shù)new Date()
: 創(chuàng)建一個表示當前日期和時間的Date對象。
整數(shù)參數(shù)new Date(milliseconds)
: 以從1970年1月1日00:00:00 UTC起經過的毫秒數(shù)來創(chuàng)建日期對象。
日期字符串參數(shù)new Date(dateString)
: 使用特定格式的字符串表示的日期和時間來創(chuàng)建Date對象。
例如: new Date("2024-03-05T12:00:00")
年、月、日等參數(shù)new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
: 使用特定的年、月、日等信息來創(chuàng)建Date對象。
月份從0開始,所以一月是0,二月是1,以此類推。
多個整數(shù)參數(shù)new Date(year, monthIndex, day)
: 使用年、月、日創(chuàng)建Date對象。
ISO 格式字符串參數(shù)new Date(ISOString)
: 使用ISO格式的字符串(符合規(guī)范的日期時間字符串)來創(chuàng)建Date對象。
例如: new Date(“2024-03-05T12:00:00Z”)
其他參數(shù)
還可以根據(jù)需要提供其他參數(shù),如小時、分鐘、秒和毫秒。
// 創(chuàng)建一個表示當前日期和時間的對象 var currentDate = new Date(); // 使用毫秒數(shù)創(chuàng)建一個日期對象 var dateFromMilliseconds = new Date(1629194400000); // 使用字符串創(chuàng)建一個日期對象 var dateFromString = new Date("2024-03-05T12:00:00"); // 使用年、月、日創(chuàng)建一個日期對象 var dateFromYMD = new Date(2024, 2, 5); // 月份從0開始,所以3月是2 // 使用 ISO 格式字符串創(chuàng)建一個日期對象 var dateFromISOString = new Date("2024-03-05T12:00:00Z");
new Date(year, month - 1, 1);
year
表示年份month-1
表示月份,這里減去1是因為JavaScript中的月份是從0開始的(0表示一月,1表示二月,以此類推)
1表示日期,這里是月份的第一天
所以,這一行代碼創(chuàng)建了一個Date對象,表示給定年份和月份的第一天。
new Date(year, month, 0);
year表示年份
month表示月份
0表示日期,但在JavaScript中,當日期為0時,它表示前一個月的最后一天
因此,這一行代碼創(chuàng)建了一個Date對象,表示給定年份和月份的最后一天。
toLocaleString
在JavaScript中,toLocaleString是用于將數(shù)字或日期格式化為本地化字符串的方法。它可以接受一些可選的參數(shù),以便更精確地控制輸出格式。
locales(可選)
類型: 字符串或字符串數(shù)組
描述: 表示一個或多個區(qū)域設置代碼,用于指定所需的語言和地區(qū)。如果傳遞多個區(qū)域設置,瀏覽器將按照列表的順序查找最佳匹配。
var number = 123456.789; console.log(number.toLocaleString('en-US')); // 輸出: 123,456.789 (使用美國英語格式) console.log(number.toLocaleString('de-DE')); // 輸出: 123.456,789 (使用德國德語格式)
options(可選)
類型: 對象
描述: 一個包含以下屬性的對象,用于更詳細地配置格式:
style: 可以是’decimal’(默認),‘currency’,或’percent’。
currency: 當style為’currency’時,表示貨幣的ISO 4217代碼。
minimumIntegerDigits: 整數(shù)部分的最小位數(shù)。
minimumFractionDigits: 小數(shù)部分的最小位數(shù)。
maximumFractionDigits: 小數(shù)部分的最大位數(shù)。
minimumSignificantDigits: 顯著數(shù)字的最小位數(shù)。
maximumSignificantDigits: 顯著數(shù)字的最大位數(shù)。
var number = 123456.789; var options = { style: 'currency', currency: 'EUR', minimumFractionDigits: 2, maximumFractionDigits: 2 }; console.log(number.toLocaleString('de-DE', options)); // 輸出: €123,456.79 (使用德國德語格式,貨幣為歐元,小數(shù)位數(shù)為兩位)
dateStyle和timeStyle(可選)
類型: 字符串
描述: 用于指定日期和/或時間的樣式,可以是"full"、“long”、“medium"或"short”。
var date = new Date(); var options = { dateStyle: 'full', timeStyle: 'short' }; console.log(date.toLocaleString('en-US', options)); // 輸出: Saturday, March 5, 2024 at 1:30 PM (使用美國英語格式)
這些參數(shù)可以單獨使用,也可以一起結合使用,以滿足特定的本地化需求。根據(jù)需要選擇適當?shù)膮?shù),并根據(jù)具體情況調整其值。
Intl
const now = new Date(); // 通過 Intl.DateTimeFormat 構造函數(shù)創(chuàng)建一個日期格式化對象,設置 timeZone 為 'Asia/Shanghai'(中國時區(qū)) const formatter = new Intl.DateTimeFormat('default', { timeZone: 'Asia/Shanghai', year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', }); // 使用格式化對象將日期對象轉換為字符串 const formattedDate = formatter.format(now); console.log(formattedDate);
到此這篇關于JavaScript之通過年月獲取月份的天數(shù)、日期格式化、時間、補零、Date、toLocaleString、Intl、DateTimeFormat、format的文章就介紹到這了,更多相關js獲取月份的天數(shù)日期格式化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS組件中bootstrap multiselect兩大組件較量
這篇文章主要介紹了JS組件中bootstrap multiselect兩大組件,兩者之間的較量,優(yōu)缺點比較,感興趣的小伙伴們可以參考一下2016-01-01