current position:Home>Convert timestamp to formatted date JS

Convert timestamp to formatted date JS

2022-05-15 07:30:54Twinkle, twinkle, twinkle, twinkle, twinkle, twinkle, twinkle,

Extract :https://www.zhangxinxu.com/php/microCodeDetail?id=10 

/*
**  The time stamp is converted to the specified format date 
** eg. 
** dateFormat(11111111111111, 'Y year m month d Japan  H when i branch ')
** → "2322 year 02 month 06 Japan  03 when 45 branch "
*/
var dateFormat = function (timestamp, formats) {
    // formats The format includes 
    // 1. Y-m-d
    // 2. Y-m-d H:i:s
    // 3. Y year m month d Japan 
    // 4. Y year m month d Japan  H when i branch 
    formats = formats || 'Y-m-d';

    var zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    var myDate = timestamp? new Date(timestamp): new Date();

    var year = myDate.getFullYear();
    var month = zero(myDate.getMonth() + 1);
    var day = zero(myDate.getDate());

    var hour = zero(myDate.getHours());
    var minite = zero(myDate.getMinutes());
    var second = zero(myDate.getSeconds());

    return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
        return ({
            Y: year,
            m: month,
            d: day,
            H: hour,
            i: minite,
            s: second
        })[matches];
    });
};

copyright notice
author[Twinkle, twinkle, twinkle, twinkle, twinkle, twinkle, twinkle, ],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/135/202205142331201979.html

Random recommended