JavaScript formatting returns the current date and time

Time:2021-2-1

requirement:

Returns the current date and time, such as

Tuesday, October 6, 2020 08:15:35 PM

Code implementation:

function getDate() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    month = month < 10 ? '0' + month : month;
    var dates = date.getDate();
    dates = dates < 10 ? '0' + dates : dates;
    Var arr = ['sunday ',' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ',' Saturday '];
    var day = date.getDay();
    var hour = date.getHours();
    Var half = hour > = 12?'afternoon ':'morning';
    hour = hour <= 12 ? hour : (hour - 12);
    hour = hour < 10 ? '0' + hour : hour;
    var min = date.getMinutes();
    min = min < 10 ? '0' + min : min;
    var sed = date.getSeconds();
    sed = sed < 10 ? '0' + sed : sed;
    Return (year +'year '+ month +'month' + dates +'day '+ arr [day] +' + Half + hour +'point '+ min +'minute' + sed +'second ');
}
console.log(getDate());

Output results:

Tuesday, October 6, 2020 08:19:02 PM

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]