JavaScript之Date对象

468 阅读4分钟

Date对象

Date对象用于处理日期和时间

创建Date对象

// 返回当前日期和时间 格林尼治时间(GMT)
var date = new Date()
console.log(date) // Fri Nov 30 2018 11:11:29 GMT+0800 (中国标准时间)

格林尼治时间(GMT)世界时,是指格林尼治所在地的标准时间。英国伦敦是0时区,北京是东八区。格林威治时间GMT,GMT+8就是北京时间
整个地球分为二十四时区,每个时区都有自己的本地时间。

Date格式化日期

// 根据毫秒数格式化时间(GMT)
var today = new Date(1543547738449)
console.log(today) // Fri Nov 30 2018 11:15:38 GMT+0800 (中国标准时间)

var today = new Date(Date.now())
console.log(today)

// 根据日期字符串格式化时间(GMT) new Date(dateString) 
new Date('2018-11-23 09:36:10') // Fri Nov 23 2018 09:36:10 GMT+0800 (中国标准时间)
new Date('2018/11/24 09:36:10') // Sat Nov 24 2018 09:36:10 GMT+0800 (中国标准时间)

// 根据年月日时分秒格式化时间
new Date(year, month, day, hours, minutes, seconds, milliseconds);
new Date(2018, 11, 24, 10, 20, 30) // Mon Dec 24 2018 10:20:30 GMT+0800 (中国标准时间)

Date.now()

代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。

// 代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。
var milliseconds = Date.now()
console.log(milliseconds) // 1543547738449

// getTime() 等价于 Date.now()
var now = new Date()
console.log(now.getTime())

Date实例的常用方法

var date = new Date() // 创建当前时间对象
console.log(date.getDate()) // 一个月中的某一天 日期
console.log(date.getDay()) // 一周中的某一天(0 ~ 6) 0代表周日
console.log(date.getMonth()) // 月份(0 ~ 11)0代表1月 11代表12月
console.log(date.getFullYear()) // 年份
console.log(date.getHours()) // 小时
console.log(date.getMinutes()) // 分钟
console.log(date.getSeconds()) // 秒
console.log(date.getMilliseconds()) // 毫秒
console.log(date.getTime()) // 和Date.now() 一样 返回1970年1月1日至今的毫秒数。

// 把date对象转换为字符串
date.toString() // Fri Nov 30 2018 15:15:55 GMT+0800 (中国标准时间)

// 把Date对象的时间部分转换为字符串
date.toTimeString() // 15:16:07 GMT+0800 (中国标准时间)

// 把 Date 对象的日期部分转换为字符串。
date.toDateString() // Fri Nov 30 2018

// 根据本地时间格式,把 Date 对象转换为字符串。
date.toLocaleString() // 2018/11/30 下午3:16:10

获取某个月份共有多少天

// 获取哪个月份就传数字几
    // var date = new Date(2018, 月份, 0)
    // date.getDate()
 
    // 这里1代表2月份 
    // 0代表上个月最后一天
    var date = new Date(2018, 1, 0)
    // 上个月的最后一天(1月份的最后一天日期)
    console.log(date.getDate()) // 31

倒计时

// 时间补零函数
function addZero(time) {
  // return time < 10 ? ('0' + time) : time
  return (time + '').padStart(2, 0)
}
// 倒计时
function countDown(targetDate) {
  var target = new Date(targetDate)
  var now = new Date()
  var diffTime = target - now
  if (diffTime <= 0) {
    window.clearInterval(timer)
    return '倒计时已经结束'
  }
  var seconds = Math.floor(diffTime / 1000)
  // 换算
  var d = 24 * 60 * 60
  var h = 60 * 60
  // 天数
  var day = Math.floor(seconds / d)
  seconds = seconds % d // 换算成天数后 剩余秒数
  // 小时
  var hour = Math.floor(seconds / h)
  seconds = seconds % h
  // 分钟
  var minutes = Math.floor(seconds / 60)
  // 秒
  var sec = Math.floor(seconds % 60)
  return `${addZero(day)}${addZero(hour)}小时${addZero(minutes)}${addZero(sec)}秒`
}
// console.log(countDown('2018/12/2 10:10:10')) // 01天18小时32分05秒

var timer = setInterval(function() {
 title.innerHTML = countDown('2019/1/10 10:10:10')
}, 1000)
function countDown(targetDate) {
      var target = new Date(targetDate)
      var now = new Date()
      var diffTime = target - now
      
      if (diffTime < 0) {
        window.clearInterval(timing)
        return
      }

      var diffSeconds = Math.floor(diffTime / 1000)
      
      var d =  60 * 60 * 24
      var h =  60 * 60

      // 天数
      var day = Math.floor(diffSeconds / d)
      diffSeconds %= d

      // 小时
      var hour = Math.floor(diffSeconds / h)
      diffSeconds %= h

      // 分钟
      var minutes = Math.floor(diffSeconds / 60)
      
      // 秒数
      var seconds = diffSeconds % 60

      return `${day}${hour}小时 ${minutes}分钟 ${seconds}秒`
      
    }

    var timer = document.getElementById('timer')
    var timing = setInterval(function() {
      timer.innerHTML = countDown('2019/03/01 00:00:00')
    }, 1000)
// 时间补零函数
function addZero(time) {
  // return time < 10 ? ('0' + time) : time
  return (time + '').padStart(2, 0)
}
// 倒计时
function countDown(targetDate) {
  var target = new Date(targetDate)
  var now = new Date()
  // 毫秒差
  var diffTime = target - now
  if (diffTime <= 0) {
    window.clearInterval(timer)
    return '倒计时已经结束'
  }
  // 将毫秒换算成秒
  var seconds = Math.floor(diffTime / 1000)
  // 换算
  var d = 24 * 60 * 60
  var h = 60 * 60
  // 天数
  var day = Math.floor(seconds / d)
  seconds %= d // 换算成天数后 剩余秒数
  // 小时
  var hour = Math.floor(seconds / h)
  seconds %= h
  // 分钟
  var minutes = Math.floor(seconds / 60)
  // 秒
  var sec = Math.floor(seconds % 60)
  return `${addZero(day)}${addZero(hour)}小时${addZero(minutes)}${addZero(sec)}秒`
}
// console.log(countDown('2018/12/2 10:10:10')) // 01天18小时32分05秒

var timer = setInterval(function() 
  title.innerHTML = countDown('2019/1/10 10:10:10')
}, 1000)

----------------------------------------------------------------------------------------------------------------
参考文章&&强烈推荐:布罗利