JavaScript工具类(九):date日期

Date


日期对象,用于处理日期和时间。

一、创建日期

new Date(argument):返回当前日期。如:Wed Jul 31 2019 16:43:03 GMT+0800。 参数可以是:

  • timestamp:时间戳。如:1564562739397
  • date:日期对象。如:Wed Jul 31 2019 16:43:03 GMT+0800
  • dateString:时间字符串。如:2019/01/302019-01-30
  • year, month, day, hours, minutes, seconds, milliseconds:指定年月日等。如:2019,0,30

:ios下,time如果是时间字符串的话,只支持以 ‘/’ 连接的时间格式,如 ‘2019/02/19’


二、常用方法

方法描述
getFullYear()返回年份
getMonth()返回月份 (0 ~ 11)
getDate()返回一个月中的某一天 (1 ~ 31)
getDay()返回一周中的某一天 (0 ~ 6)
getHours()返回小时 (0 ~ 23)
getMinutes()返回分钟 (0 ~ 59)
getSeconds()返回秒数 (0 ~ 59)
getTime()返回 1970 年 1 月 1 日至今的毫秒数
setFullYear()设置年份(四位数字)
setMonth(month, day)设置月份 (0 ~ 11)。day 为 0 时,Date 对象为上个月的最后一天
setDate()设置月的某一天 (1 ~ 31)
setTime()以毫秒设置 Date 对象
toUTCString()根据世界时间 (UTC) 把 Date 对象转换为字符串,并返回结果。UTC时间 与 GMT(格林尼治)时间一样
Date.parse(dateString)返回该字符串所表示的日期与 1970 年 1 月 1 日午夜之间相差的毫秒数

date 日期方法封装

u.date = {}

获取需要的时间格式
/**
 * @description 获取需要的时间格式
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'。如果是'星期WW',则返回(如:'星期日')
 * @return {String} 格式化后的时间
 */
u.date.format = function(time, format) {
    time = time ? new Date(time) : new Date()
    format = format || 'YYYY-MM-DD'
    function tf(i) { return (i < 10 ? '0' : '') + i }
    return format.replace(/YYYY|MM|DD|hh|mm|ss|WW/g, function(a) {
        switch (a) {
                case 'YYYY':
                    return tf(time.getFullYear())
                case 'MM':
                    return tf(time.getMonth() + 1)
                case 'DD':
                    return tf(time.getDate())
                case 'mm':
                    return tf(time.getMinutes())
                case 'hh':
                    return tf(time.getHours())
                case 'ss':
                    return tf(time.getSeconds())
                case 'WW':
                    return ['日', '一', '二', '三', '四', '五', '六'][time.getDay()]
        }
    })
}
获取前后几月的日期
/**
 * @description 获取前后几月的日期
 * @param {Number} MM 前后几月(正数代表后几个月,负数代表前几个月),默认上个月(-1)
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'
 * @return {String} 格式化后的时间
 */
u.date.otherMonth = function(MM, time, format) {
    MM = !isNaN(parseInt(MM)) ? parseInt(MM) : -1
    time = time ? new Date(time) : new Date()

    var oldDate = time.getDate()
    time.setMonth(time.getMonth() + MM)
    var newDate = time.getDate()
    if (newDate < oldDate) {
        time.setMonth(time.getMonth(), 0)
    }
    return u.date.format(time, format)
}
某一月的第一天
/**
 * @description 某一月的第一天
 * @param {Number} MM 前后几月(正数代表后几个月,负数代表前几个月),默认本月(0)
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'
 * @return {String} 格式化后的时间
 */
u.date.startOfMonth = function(MM, time, format) {
    MM = !isNaN(parseInt(MM)) ? parseInt(MM) : 0
    time = time ? new Date(time) : new Date()

    time.setMonth(time.getMonth() + MM, 1)
    return u.date.format(time, format)
}
某一月的最后一天
/**
 * @description 某一月的最后一天
 * @param {Number} MM 前后几月(正数代表后几个月,负数代表前几个月),默认本月(0)
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'
 * @return {String} 格式化后的时间
 */
u.date.endOfMonth = function(MM, time, format) {
    MM = !isNaN(parseInt(MM)) ? parseInt(MM) : 0
    time = time ? new Date(time) : new Date()

    time.setMonth(time.getMonth() + MM + 1, 0)
    return u.date.format(time, format)
}
某一周的第一天(默认星期一)
/**
 * @description 某一周的第一天(默认星期一)
 * @param {Number} WW 前后几周(正数代表后几周,负数代表前几周),默认本周(0)
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'
 * @return {String}
 */
u.date.startOfWeek = function(WW, time, format) {
    WW = !isNaN(parseInt(WW)) ? parseInt(WW) : 0
    time = time ? new Date(time) : new Date()

    var curWW = time.getDay()
    curWW = curWW === 0 ? 7 : curWW
    time.setDate(time.getDate() + 7 * WW - (curWW - 1))
    return u.date.format(time, format)
}
某一周的最后一天(默认星期日)
/**
 *@description 某一周的最后一天(默认星期日)
 * @param {Number} WW 前后几周(正数代表后几周,负数代表前几周),默认本周(0)
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'
 * @return {String}
 */
u.date.endOfWeek = function(WW, time, format) {
    WW = !isNaN(parseInt(WW)) ? parseInt(WW) : 0
    time = time ? new Date(time) : new Date()

    var curWW = time.getDay()
    curWW = curWW === 0 ? 7 : curWW
    time.setDate(time.getDate() + 7 * WW - (curWW - 1) + 6)
    return u.date.format(time, format)
}
前后几天的日期(几小时、几分钟均可)
/**
 * @description 前后几天的日期(几小时、几分钟均可)
 * @param {Number} DD 前后几天(正数代表后几天,负数代表前几天),默认过去一周的日期(-6)
 * @param {Date} time 时间、时间字符串、时间戳
 * @param {String} format 时间格式,默认'YYYY-MM-DD'
 * @return {String} 格式化后的时间
 */
u.date.otherDate = function(DD, time, format) {
    DD = !isNaN(parseFloat(DD)) ? parseFloat(DD) : -6
    time = time ? new Date(time) : new Date()

    time.setTime(time.getTime() + DD * (24 * 3600 * 1000))
    return u.date.format(time, format)
}
两个日期之间相差多少天
/**
 *@description 两个日期之间相差多少天
 * @param {Date} date1
 * @param {Date} date2
 * @return {Number}
 */
u.date.howManyDays = function(date1, date2) {
    var ret = ''
    var timestamp1 = Date.parse(date1)
    var timestamp2 = Date.parse(date2)
    var dateSpan = Math.abs(timestamp2 - timestamp1)
    ret = Math.floor(dateSpan / (24 * 3600 * 1000))
    return ret
}
两个日期之间相差多少月
/**
 * @description 两个日期之间相差多少月
 * @param {Date} date1
 * @param {Date} date2
 * @return {Number}
 */
u.date.howManyMonths = function(date1, date2) {
    var months1, months2, ret
    date1 = new Date(date1)
    date2 = new Date(date2)
    months1 = date1.getFullYear() * 12 + date1.getMonth() + 1
    months2 = date2.getFullYear() * 12 + date2.getMonth() + 1
    ret = Math.abs(months1 - months2)
    return ret
}
查询两个日期之间的所有日期
/**
 *@description 查询两个日期之间的所有日期
 * @param {Date} date1
 * @param {Date} date2
 * @return {Array}
 */
u.date.getDatesBetween = function(date1, date2, format) {
    format = format || 'YYYY-MM-DD'

    var start, len
    var ret = []
    start = Date.parse(date1) < Date.parse(date2) ? date1 : date2
    // 所有天
    if (format.indexOf('DD') > -1) {
        len = u.date.howManyDays(date1, date2)
        for (var i = 0; i <= len; i++) {
            ret.push(u.date.otherDate(i, start, format))
        }
    }
    // 所有月
    else {
        len = u.date.howManyMonths(date1, date2)
        for (var j = 0; j <= len; j++) {
            ret.push(u.date.otherMonth(j, start, format))
        }
    }
    return ret
}

下载

npm i sg-utils -S

GitHub地址(记得给星哦)

https://github.com/shiguang0116/sg-utils


系列文章

JavaScript工具类(一):util.js创建及上传
JavaScript工具类(二):cookie缓存
JavaScript工具类(三):localStorage本地储存
JavaScript工具类(四):数据类型
JavaScript工具类(五):string字符串
JavaScript工具类(六):number数字
JavaScript工具类(七):array数组
JavaScript工具类(八):object对象
JavaScript工具类(九):date日期
JavaScript工具类(十):base64编码、解码
JavaScript工具类(十一):浏览器、移动端类型
JavaScript工具类(十二):validate表单验证
JavaScript工具类(十三):url路径处理
JavaScript工具类(十四):json数据格式
JavaScript工具类:util.js用法实例


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值