js日期解析和格式化相关函数

js日期格式化和解析相关函数

/** 日期格式化为字符串 */
let formatDate = function(date,fmt){
    var o = {
        "M+" : date.getMonth() + 1, // 月份
        "d+" : date.getDate(), // 日
        "H+" : date.getHours(), // 小时
        "m+" : date.getMinutes(), // 分
        "s+" : date.getSeconds(), // 秒
        "q+" : Math.floor((date.getMonth() + 3) / 3), // 季度
        "S+" : date.getMilliseconds()
    // 毫秒
    };
    var rst = /(y+)/.exec(fmt);
    rst
            && (fmt = fmt.replace(rst[1], (date.getFullYear() + '')
                    .substr(4 - rst[1].length)));
    for ( var k in o) {
        rst = new RegExp("(" + k + ")").exec(fmt);
        rst
                && (fmt = fmt.replace(rst[1], rst[1].length == 1 ? o[k]
                        : ('000'.substr(0, rst[1].length
                                - ('' + o[k]).length) + o[k])));
    }
    return fmt;
};
/**
 * 要求:如果指定毫秒ms,则必须指定全部字段; 如果指定second字段,
 * 则必须指定从year到second的所有字段;
 * 例如:不能只指定时分秒而不指定日月年。
 * 否则会出现非预期结果。这是为了程序的一致性故意设计的,
 * 使结果与当前日期没有任何关系。
 */
let parseDate = function(str,fmt){
    var reg = {
            year : /(y+)/,
            month:/(M+)/,
            day:/(d+)/,
            hour:/(H+)/,
            minute:/(m+)/,
            second:/(s+)/, 
            ms:/(S+)/ 
        };
        var date = new Date();

        var rst = reg.year.exec(fmt);
        var year = rst?+str.substr(rst.index,rst[1].length):null;
        date.setFullYear(year);

        rst = reg.month.exec(fmt);
        var month = rst?+str.substr(rst.index,rst[1].length)-1:0;   
        rst = reg.day.exec(fmt);
        var day = rst?+str.substr(rst.index,rst[1].length):1;
        date.setMonth(month,day);

        rst = reg.hour.exec(fmt);
        var hour = rst?+str.substr(rst.index,rst[1].length):0;
        date.setHours(hour);

        rst = reg.minute.exec(fmt);
        var minute = rst?+str.substr(rst.index,rst[1].length):0;
        date.setMinutes(minute);

        rst = reg.second.exec(fmt);
        var second = rst?+str.substr(rst.index,rst[1].length):0;
        date.setSeconds(second);

        rst = reg.ms.exec(fmt);
        var ms = rst?+str.substr(rst.index,rst[1].length):0;
        date.setMilliseconds(ms);

        return date;
};
/** 日期加上天数,返回新的日期 */
/** 日期加上天数,返回新的日期 */
let plusDays = function(date,daynum){
    var ms = date.getTime() + daynum * 24 * 60 * 60 * 1000
    var newdate = new Date()
    newdate.setTime(ms)
    return newdate
}
let plusMonths = function(date,monthnum){
    const newdate = new Date(date)
    newdate.setMonth(newdate.getMonth()+monthnum)
    return newdate
}

let plusMinutes = function(date,minutenum){
    var ms = date.getTime() + minutenum * 60 * 1000
    var newdate = new Date()
    newdate.setTime(ms)
    return newdate
}
/**
 * @param Date
 * @return Date
   注意:礼拜天为一周的第一天。
 * 精确到天
 * */
let atStartOfWeek = function(date){
    /*const str = date.toString().substr(0,3)
    //取日期字符串的前3个字符
    const weekdays = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
    const index = weekdays.indexOf(str)
    return plusDays(date,-index)*/
    return plusDays(date,-date.getDay())
}
/**
 * 精确到天
 * */
let atStartOfMonth = function(date){
    return plusDays(date,1-date.getDate())
}
/**
 * 精确到月
 * */
let atStartOfQuarter = function(date){
    const month = date.getMonth()//0~11
    const r = Math.floor(month/3)
    const newdate = new Date()
    newdate.setTime(date.getTime())
    newdate.setMonth(r*3)
    return newdate
}
/**
 * 精确到月
 * */
let atStartOfHalfYear = function(date){
    const month = date.getMonth()//0~11
    const r = Math.floor(month/6)
    const newdate = new Date()
    newdate.setTime(date.getTime())
    newdate.setMonth(r*6)
    return newdate
}
/**
 * 精确到月
 * */
let atStartOfYear = function(date){
    const newdate = new Date()
    newdate.setTime(date.getTime())
    newdate.setMonth(0)
    return newdate
}

猜你喜欢

转载自blog.csdn.net/zhoujiaping123/article/details/80385920
今日推荐