Story time and time stamp

 

 

First, the meaning of the timestamp

 First, many of my friends may not understand the meaning of the timestamp, I was introduced to Kazakhstan

Time stamp refers GMT January 1, 1970 00 hours 00 minutes 00 seconds (Beijing time on January 1, 1970 08 hours 00 minutes 00 seconds) until now the total number of seconds.

Let's understand the roots of his can continue to play anymore

Second, the time is converted to time stamp

1.js get the current timestamp method

var timestamp1 = Date.parse(new Date());//不推荐这种办法,毫秒级别的数值被转化为000
var timestamp2 = (new Date()).valueOf();//获取了当前毫秒的时间戳,准确。
var timestamp3 = new Date().getTime();//getTime()返回数值的单位是毫秒

Gets the timestamp method 2.js

var timestamp4= (new Date("2018/06/22 08:00:20")).getTime()/1000;

 

Third, the time stamp is converted to time

var timestamp = new Date (1472048779952) // directly New Date (to convert the time stamp) to get the date stamp represents the

Note that the time stamp which must be a type Int, otherwise the conversion will fail. ParseInt () look best.

 

 

Fourth, the date format

Long used in the development of the date, but you new Date () can not be directly used to get or displayed to the user, so in general we will write a date formatting method

as follows:

function dateFormat(thisDate, fmt) {
    var o = {
        "M+": thisDate.getMonth() + 1,
        "d+": thisDate.getDate(),
        "h+": thisDate.getHours(),
        "m+": thisDate.getMinutes(),
        "s+": thisDate.getSeconds(),
        "q+": Math.floor((thisDate.getMonth() + 3) / 3),
        "S": thisDate.getMilliseconds()
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (thisDate.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 Date=new Date();
console.log(dateFormat(Date, "yyyy-MM-dd hh:mm:ss"))//调用

 

Fifth, is calculated based on the date of the week

 


1. Calculate the current date Sunday and Monday

 var Date=new Date();
 var selectTime = Date.getTime();//获取时间戳
    var selectDay = Date.getDay();//获取星期
    var oneDayLong = 24 * 3600 * 1000;//定义一天有多少毫秒
    var MondayTime = selectTime - (selectDay - 1) * oneDayLong;
  var monday = new Date(MondayTime)//时间戳再转回时间
  var SundayTime = selectTime - (selectDay - 7) * oneDayLong;
  var sundayTime = new Date(SundayTime)

console.log(monday)
console.log(sundayTime)

Sixth, the calculation of a day of the week

When we know that a 'yyyy-mm-dd' date will be able to calculate the current day of the week, using the following formula:

基姆拉尔森计算公式
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
在公式中d表示日期中的日数,m表示月份数,y表示年数。(其中mod意思是取余计算,js用的是%)

 

Published 25 original articles · won praise 10 · views 30000 +

Guess you like

Origin blog.csdn.net/xj932956499/article/details/80759903
Recommended