当前位置:网站首页>Time and date processing in JS

Time and date processing in JS

2022-06-13 08:30:00 SwJieJie

1, Common methods of processing date :

var myDate = new Date();
myDate.getYear(); // Get current year (2 position )
myDate.getFullYear(); // Get full year (4 position ,1970-????)
myDate.getMonth(); // Get current month (0-11,0 representative 1 month )
myDate.getDate(); // Get current day (1-31)
myDate.getDay(); // Get current week X(0-6,0 For Sunday )
myDate.getTime(); // Get the current time ( from 1970.1.1 Milliseconds to start )
myDate.getHours(); // Get current hours (0-23)
myDate.getMinutes(); // Get current minutes (0-59)
myDate.getSeconds(); // Get current seconds (0-59)
myDate.getMilliseconds(); // Get current milliseconds (0-999)
myDate.toLocaleDateString(); // Get current date 
var mytime=myDate.toLocaleTimeString(); // Get the current time 
myDate.toLocaleString( ); // Get date and time 

2,js Get current date , And format it as YYYY-MM-DD

(1), Method 1 : Common code to handle

//  Get current date directly 
 function getNowFormatDate() {
    
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
    
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
    
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
 perhaps 

//  Get the current date according to the timestamp 
function transformationDate (date) {
    
  const Month = date.getMonth() + 1
  return `${
      date.getFullYear()}-${
      Month > 9 ? Month : `0${
        Month}`}-${
      date.getDate() > 9 ? date.getDate() : `0${
        date.getDate()}`}`
}
function getLocalTime(nS) {
         
		var d = new Date(parseInt(nS)* 1000);    // Time objects generated from timestamps 
		var date = (d.getFullYear()) + "-" + 
					(d.getMonth() + 1) + "-" +
					(d.getDate()) + " " + 
					(d.getHours()) + ":" + 
					(d.getMinutes()) + ":" + 
					(d.getSeconds()); 
		return date;   
}
document.write(getLocalTime(1642402680905));
console.log(transformationDate(new Date(1642402680905)))
//  result : 2022-01-17

 perhaps 
//  Get the current time and format "yyyy-MM-dd HH:mm:ss"
Date.prototype.Format = function (fmt) {
    
    var o = {
    
        "M+": this.getMonth() + 1, // month  
        "d+": this.getDate(), // Japan  
        "H+": this.getHours(), // Hours  
        "m+": this.getMinutes(), // branch  
        "s+": this.getSeconds(), // second  
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
        "S": this.getMilliseconds() // millisecond  
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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;
}
console.log(new Date().Format("yyyy-MM-dd"))
//  result :2022-01-17
console.log(new Date().Format("yyyy-MM-dd HH:mm:ss"))
//  result :2022-01-17 14:55:10

Method 2 : The use of plug-in moment.js To deal with

1, install moment.js The library of
npm install moment --save

  moment().format('YYYY-MM-DD')

3, Convert date time string to timestamp


var date = '2015-03-05 17:59:00.0';
date = date.substring(0,19);    
date = date.replace(/-/g,'/'); 
var timestamp = new Date(date).getTime();
document.write(timestamp);

4, Current time to timestamp

var timestamp = parseInt(new Date().getTime()/1000);    //  Current timestamp 
document.write(timestamp);

5, Get the corresponding day of the week according to the timestamp

getWeek (time) {
    
    var d = new Date(time).getDay()
    const weekDay = [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday ']
    return `${
      weekDay[d]}`
  }
原网站

版权声明
本文为[SwJieJie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270542026159.html