This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

πŸ’ͺ~ If you like it, you can like it or leave a message πŸ’•~~~, thank you ⭐️ πŸ’• ️~~

1. Timestamp conversion (10 digits)/(13 digits)

// The timestamp is 13 bits
formatDate: function (time) {// Time stamp to date
    let date = new Date(time);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + The '-' + MM + The '-' + d + ' ' + h + ':' + m + ':' + s;
    // return y + '-' + MM + '-' + d;
},
// The timestamp is 10 bits
formatDate: function (time) {// Time stamp to date
    let date = new Date(parseInt(time) * 1000);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + The '-' + MM + The '-' + d + ' ' + h + ':' + m + ':' + s;
    // return y + '-' + MM + '-' + d;
},
Copy the code

2. Timestamp to date time (customizable return format)

Here USES the JavaScript function library jutils formatDate function, reference address jutils attach: < script SRC = “https://cdn.jsdelivr.net/npm/jutils-src” > < / script >

function formatDate(data,formatstr){
    var arrweek=["Day"."δΈ€"."二"."Three"."Four"."Five"."Six"];
    var str=formatstr.replace(/yyyy|YYYY/,date.getFullYear()).replace(/yy|YY/,
    $addZero(date.getFullYear() % 100.2)).replace(/mm|MM/,$addZero(date.getMonth() + 1.2)).replace(/m|M/g,date.getMonth() + 1).replace(/dd|DD/,$addZero(date.getDate(),2)).replace(/d|D/g,
    date.getDate()).replace(/hh|HH/,$addZero(date.getHours(),2)).replace(/h|H/g,
    date.getHours()).replace(/ii|II/,$addZero(date.getMinutes(),2)).replace(/i|I/g,
    date.getMinutes()).replace(/ss|SS/,$addZero(date.getSeconds(),2)).replace(/s|S/g,
    date.getSeconds()).replace(/w|g/,$addZero(date.getDay(),2)).replace(/W/g,arrweek[date.getDay()]);
    return str;
}
function $addZero(v,size){
  for(var i=0,len=size-(v+"").length; i<len; i++){ v="0"+v;
  }
  return v+""} Example// year, month, day, hour, minute, second
var date = jutils.formatDate(new Date(1634002035*1000),"YYYY-MM-DD HH:ii:ss");
/ / the 2021-10-12 09:27:15

// year, month, day, week
var date = jutils.formatDate(new Date(1634002035*1000),"YYYY - MM - DD weeks W");
/ / the 2021-10-12 on Tuesday

// time, minute, second
var date = jutils.formatDate(new Date(1634002035*1000),"HH:ii:ss");
/ / 09:27:15
Copy the code

3. Method of obtaining the current timestamp

var times = Date.parse(new Date());// Not recommended, because milliseconds are converted to 000, which is not accurate!

var times = (new Date()).valueOf();// Get the timestamp of the current millisecond.

var times = new Date().getTime();// Returns a value in milliseconds;
Copy the code

4. Time to timestamp millisecond method

(new Date(this.zzsj)).getTime()   //getTime() returns a value in milliseconds
Copy the code

5. Time conversion to timestamp

DateThe parse ()// Go to the timestamp
Copy the code

6. Get the current year, month and day

getDatetime(){
    // Get the current date
    let date_ = new Date(a);let seperator1 = "-";
    let year = date_.getFullYear();
    let month = date_.getMonth() + 1;
    let strDate = date_.getDate();
}
Copy the code

7.Js gets the current date, time and other operations

var Date = new Date(a);Date.getYear();        // Get the current year (2 bits)
Date.getFullYear();    // Get the full year (4 bits,1970-????)
Date.getMonth();       // Get the current month (0-11,0 for January)
Date.getDate();        // Get the current day (1-31)
Date.getDay();         // Get the current week X(0-6,0 for Sunday)
Date.getTime();        // Get the current time (milliseconds from 1970.1.1)
Date.getHours();       // Get the current hour (0-23)
Date.getMinutes();     // Get the current minute (0-59)
Date.getSeconds();     // Get the current number of seconds (0-59)
Date.getMilliseconds();    // Get the current number of milliseconds (0-999)
Date.toLocaleDateString();     // Get the current date
var time=Date.toLocaleTimeString();     // Get the current time
Date.toLocaleString( );        // Get the date and time
Copy the code

Comments on the lottery

Feel free to discuss in the comments section. The nuggets will be sending out 100 nuggets in the comments section after the diggnation campaign

Raffle gift: 100 pieces, including badges, slippers, mugs, canvas bags, etc., will be distributed at random

Drawing time: Within 3 working days after the end of project Diggnation, all articles that meet the rules will be officially posted in the comments section

Lucky draw: Nuggets official random draw + manual verification

Comment content: comments, suggestions and discussions related to the content of the article. General comments such as “treading on” and “learning” will not win the prize

Column recommended

Recommend their own column, welcome everyone to collect attention to 😊~

  • Set the interview
  • Native js set
  • Vue set
  • Visual set
  • CSS set