chiwent / blog

个人博客,只在issue内更新
https://chiwent.github.io/blog
8 stars 0 forks source link

JavaScript中的日期操作 #9

Open chiwent opened 5 years ago

chiwent commented 5 years ago

JavaScript中的日期操作

在介绍js的日期操作前,首先要理解一些基本的概念:


js中的Date对象可以通过普通函数的形式直接调用,无论是否有参数,返回值都是当前时间的GMT格式:

Date()
// "Thu Mar 14 2019 23:19:38 GMT+0800 (中国标准时间)"

Date(2000, 1, 1)
// "Thu Mar 14 2019 23:19:38 GMT+0800 (中国标准时间)"

js的Date对象也可以通过构造函数的形式调用,如果不加参数则返回当前的时间的GMT格式;如果添加参数,返回值就是参数对应的GMT时间:

new Date(1548218728000)
// Wed Jan 23 2019 12:45:28 GMT+0800 (中国标准时间)
// 如果毫秒时间戳是负数,那么返回值是1970年1月1日之前的时间

new Date('March, 3, 2019')
// Sun Mar 03 2019 00:00:00 GMT+0800 (中国标准时间)

new Date('3, 3, 2019')
// Sun Mar 03 2019 00:00:00 GMT+0800 (中国标准时间)

new Date('2019, 3, 3')
// Sun Mar 03 2019 00:00:00 GMT+0800 (中国标准时间)

new Date(2019, 2, 3, 12, 30, 30, 0)
// Sun Mar 03 2019 12:30:30 GMT+0800 (中国标准时间)

new Date('2019-3-3')
new Date('2019/3/3')
new Date('3/3/2019')
// ......

只要是能被Date.parse()解析的字符串,都可以用作参数

一个小提示:如果没有将new Date()赋予一个变量,那么返回值类型是object,当赋予一个变量后,改变量会默认调用toString方法转换为字符串。

一些小坑:

一些方法

相关的API查看:http://www.w3school.com.cn/js/jsref_obj_date.asp

抽取其中的一些进行说明:

日期格式化相关的一些代码片段

// from:http://caibaojian.com/javascript-date-format.html

// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
    return fmt;
}

// 调用:
var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
// from:https://www.fedte.cc/p/622.html
var date = new Date();

function moment(context){
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var now = new Date().getTime();
    function getDateDiff(dateTimeStamp) {
    var diffValue = now - dateTimeStamp;
    if (diffValue < 0) {
        result = new Date(dateTimeStamp).toLocaleDateString();
    }
    var yearComment = diffValue /(month * 12)
    var monthComment = diffValue / month;
    var weekComment = diffValue / (7 * day);
    var dayComment = diffValue / day;
    var hourComment = diffValue / hour;
    var minComment = diffValue / minute;
    if(yearComment >= 1){
        result = parseInt(yearComment) + "年前";
    } else if (monthComment < 4 && monthComment >= 1) {
        result = parseInt(monthComment) + "个月前";
    } else if (weekComment >= 1) {
        result = parseInt(weekComment) + "周前";
    } else if (dayComment >= 1) {
        result = parseInt(dayComment) + "天前";
    } else if (hourComment >= 1) {
        result = parseInt(hourComment) + "小时前";
    } else if (minComment >= 1) {
        result = parseInt(minComment) + "分钟前";
    } else if(minComment < 0){
        result = "还未发生"
    } else
        result = "刚刚";
    return result;
    }
    return getDateDiff(Date.parse(context))
}

参考: