js time of operation

Let's look at how to get the current time:

var date = new Date()
//输出:Tue Jul 02 2019 10:36:22 GMT+0800 (中国标准时间)

Then, we have to get the relevant parameters

var date = new Date();
    console.log("今天的日期是:" + date)
    //今天的日期是:Tue Jul 02 2019 10:43:55 GMT+0800 (中国标准时间)
    var year = date.getFullYear(); //获取当前年份
    console.log("今年是:" + year)
    // 今年是:2019
    var mon = date.getMonth() + 1; //获取当前月份  
    console.log("这个月是:" + mon)
    // 这个月是:7
    var day = date.getDate(); //获取当前日  
    console.log("今天是这个月的第" + day + "天")
    // 今天是这个月的第2天
    var weekday = date.getDay(); //获取当前星期几  
    console.log("今天是这周的第" + weekday + "天")
    // 今天是这周的第2天
    var h = date.getHours(); //获取小时  
    console.log("现在是:" + h + "点")
    // 现在是:10点
    var m = date.getMinutes(); //获取分钟  
    console.log("当前是这个小时:" + m + "分钟")
    // 当前是这个小时:43分钟
    var s = date.getSeconds(); //获取秒  
    console.log("当前是这个分种:" + s + "秒")
    // 当前是这个分种:55秒  

Js probably more than in the corresponding time parameters to obtain all of the methods of the bar.
But very often, we use to calculate the time difference between the two, problems often arise because they do not match the format, as long as we remember three things:

  • Between the strings can not do the math
  • The difference between the two types are doing the same, and that is the Date object Date object, timestamp (long) and a time stamp
    Consider the following:

    1. Date object converted to other formats

通过传入一个会被JavaScript解析的字符串来构造
console.log(new Date('September 7, 2018'))  // Fri Sep 07 2018 00:00:00 GMT+0800 (中国标准时间)
console.log(new Date('September 7, 2018, GMT+0800'))  // Fri Sep 07 2018 00:00:00 GMT+0800 (中国标准时间)

通过传入一个毫秒数来构造
// 从Unix新纪元的时间创建日期
console.log(new Date(0))  // Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date(10000))  // Thu Jan 01 1970 08:00:10 GMT+0800 (中国标准时间)
console.log(new Date(1536307550023))  // Fri Sep 07 2018 16:05:50 GMT+0800 (中国标准时间)
// 使用负数创建新纪元之前的日期
console.log(new Date(-1536307550023))  // Tue Apr 26 1921 23:54:09 GMT+0800 (中国标准时间)

通过传入一个特定的本地日期来构造
总的来说格式为: new Date(年, 月, 日, 时, 分, 秒)
// 月份是从0开始的,一月为0,二月为1,九月为8等等
console.log(new Date(2018, 8))  // Sat Sep 01 2018 00:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2018, 8, 7))  // Fri Sep 07 2018 00:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2018, 8, 7, 16))  // Fri Sep 07 2018 16:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2018, 8, 7, 16, 7))  // Fri Sep 07 2018 16:07:00 GMT+0800 (中国标准时间)
console.log(new Date(2018, 8, 7, 16, 7, 50))  // Fri Sep 07 2018 16:07:50 GMT+0800 (中国标准时间)
console.log(new Date(2018, 8, 7, 16, 7, 50, 23))  // Fri Sep 07 2018 16:07:50 GMT+0800 (中国标准时间)

Jane switched over part of the code book, if infringement, please contact deleted

The last time to say a localized approach

var date = new Date();
date.toLocaleString('zh-Hans-CN', {
    timeZone: "Asia/Beijing", 
    hourCycle: "h24",
    weekday: 'long', 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric'
})

More than part of the code transferred from github, if infringement, please contact deleted

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/11119350.html