js-日期转换格式

268 阅读2分钟

日期转换格式

1.固定日期格式转为时间戳
var date = '2022-05-11';
var dateTimestamp = Date.parse(date);
console.log(dateTimestamp); //1652227200000
// 或者
var date = new Date();
var dateTimestamp = date.getTime();
2.自定义日期格式
Date.prototype.format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
    str = str.replace(/M/g, (this.getMonth() + 1));
    str = str.replace(/w|W/g, Week[this.getDay()]);
    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());
    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());
    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());
    return str;
};
var date = new Date();
console.log(date.format('YYYY年MM月DD日 hh:mm:ss 星期w')); //2022年05月11日 16:26:23 星期三
3. 某个月的第一天
function firstDate(date) { 
    return new Date(date.getFullYear(), date.getMonth(), 1);//年、月、日(指定某月的第一天)
}

var date = firstDate(new Date());
console.log(date);//Sun May 01 2022 00:00:00 GMT+0800 (中国标准时间)
4. 某个月的最后一天
function lastDate(date) { 
    // 当前月份+1,然后计算上一个月(当前月)的最后一天的日期得到当前月的天数
    return new Date(date.getFullYear(), date.getMonth()+1, 0);
}

var date = lastDate(new Date());
console.log(date);//Tue May 31 2022 00:00:00 GMT+0800 (中国标准时间)
5. 某个月所在季度的第一天
function firstDateInQuarter(date) {
    //~~作用是将数字转化为32位有符号整数 舍去小数不做四舍五入
    return new Date(date.getFullYear(), ~~(date.getMonth()/3)*3, 1);
}
date = firstDateInQuarter(new Date());
console.log(date); //Fri Apr 01 2022 00:00:00 GMT+0800 (中国标准时间) 
6. 某个月所在季度的最后一天
function lastDateInQuarter(date) {
    return new Date(date.getFullYear(), ~~(date.getMonth()/3)*3 + 3, 0);
}
date = lastDateInQuarter(new Date());//Thu Jun 30 2022 00:00:00 GMT+0800 (中国标准时间)
console.log(date);
7. 某个月份的天数
function daysInMonth(date) {
    // 通过将当前月份+1,然后计算上一个月(当前所在月)的最后一天的日期就得到当前月份的天数
    return new Date(date.getFullYear(), date.getMonth()+1, 0).getDate();
}
date = daysInMonth(new Date());
console.log(date); //31
8. 某一天是星期几
var date = new Date();
var week = ['日', '一', '二', '三', '四', '五', '六'];
console.log('今天是星期' + week[date.getDay()]); //今天是星期三
9. 判断是否是闰年
function isLeapYear(date) {
    // 计算当年的第二个月的最后一天是否等于29,若是则是闰年
    return new Date(date.getFullYear(), 2, 0).getDate() == 29;
}
date = isLeapYear(new Date());
console.log(date); //false
10. 计算出相隔天数
function datePeriod(start, end) {
    return Math.abs(start - end) / 60 / 60 / 1000 / 24;
}
var start = new Date(1970, 1, 1);
console.log(start)
var end = new Date(2022,5,11);
date = datePeriod(start, end);
console.log(date); //19123

参考链接:www.cnblogs.com/strick/p/63…