JavaScript之通过年月获取月份的天数、日期格式化、时间、补零、Date、toLocaleString、Intl、DateTimeFormat、format(问题总结)
作者:牧码人MJ682517
版本一
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构造函数可以接受多种参数,这些参数用于初始化Date对象,从而表示特定的日期和时间。
无参数new Date(): 创建一个表示当前日期和时间的Date对象。
整数参数new Date(milliseconds): 以从1970年1月1日00:00:00 UTC起经过的毫秒数来创建日期对象。
日期字符串参数new Date(dateString): 使用特定格式的字符串表示的日期和时间来创建Date对象。
例如: new Date("2024-03-05T12:00:00")
年、月、日等参数new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]): 使用特定的年、月、日等信息来创建Date对象。
月份从0开始,所以一月是0,二月是1,以此类推。
多个整数参数new Date(year, monthIndex, day): 使用年、月、日创建Date对象。
ISO 格式字符串参数new Date(ISOString): 使用ISO格式的字符串(符合规范的日期时间字符串)来创建Date对象。
例如: new Date(“2024-03-05T12:00:00Z”)
其他参数
还可以根据需要提供其他参数,如小时、分钟、秒和毫秒。
// 创建一个表示当前日期和时间的对象
var currentDate = new Date();
// 使用毫秒数创建一个日期对象
var dateFromMilliseconds = new Date(1629194400000);
// 使用字符串创建一个日期对象
var dateFromString = new Date("2024-03-05T12:00:00");
// 使用年、月、日创建一个日期对象
var dateFromYMD = new Date(2024, 2, 5); // 月份从0开始,所以3月是2
// 使用 ISO 格式字符串创建一个日期对象
var dateFromISOString = new Date("2024-03-05T12:00:00Z");new Date(year, month - 1, 1);year表示年份month-1表示月份,这里减去1是因为JavaScript中的月份是从0开始的(0表示一月,1表示二月,以此类推)
1表示日期,这里是月份的第一天
所以,这一行代码创建了一个Date对象,表示给定年份和月份的第一天。
new Date(year, month, 0);
year表示年份
month表示月份
0表示日期,但在JavaScript中,当日期为0时,它表示前一个月的最后一天
因此,这一行代码创建了一个Date对象,表示给定年份和月份的最后一天。
toLocaleString
在JavaScript中,toLocaleString是用于将数字或日期格式化为本地化字符串的方法。它可以接受一些可选的参数,以便更精确地控制输出格式。
locales(可选)
类型: 字符串或字符串数组
描述: 表示一个或多个区域设置代码,用于指定所需的语言和地区。如果传递多个区域设置,浏览器将按照列表的顺序查找最佳匹配。
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: 整数部分的最小位数。
minimumFractionDigits: 小数部分的最小位数。
maximumFractionDigits: 小数部分的最大位数。
minimumSignificantDigits: 显著数字的最小位数。
maximumSignificantDigits: 显著数字的最大位数。
var number = 123456.789;
var options = {
style: 'currency',
currency: 'EUR',
minimumFractionDigits: 2,
maximumFractionDigits: 2
};
console.log(number.toLocaleString('de-DE', options));
// 输出: €123,456.79 (使用德国德语格式,货币为欧元,小数位数为两位)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 (使用美国英语格式)这些参数可以单独使用,也可以一起结合使用,以满足特定的本地化需求。根据需要选择适当的参数,并根据具体情况调整其值。
Intl
const now = new Date();
// 通过 Intl.DateTimeFormat 构造函数创建一个日期格式化对象,设置 timeZone 为 'Asia/Shanghai'(中国时区)
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之通过年月获取月份的天数、日期格式化、时间、补零、Date、toLocaleString、Intl、DateTimeFormat、format的文章就介绍到这了,更多相关js获取月份的天数日期格式化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
