Javascript add leading zeroes to date

后端 未结 25 1815
执笔经年
执笔经年 2020-11-22 02:50

I\'ve created this script to calculate the date for 10 days in advance in the format of dd/mm/yyyy:

var MyDate = new Date();
var MyDateString = new Date();
M         


        
相关标签:
25条回答
  • 2020-11-22 03:19

    You can define a "str_pad" function (as in php):

    function str_pad(n) {
        return String("00" + n).slice(-2);
    }
    
    0 讨论(0)
  • 2020-11-22 03:20

    try this for a basic function, no libraries required

    Date.prototype.CustomformatDate = function() {
     var tmp = new Date(this.valueOf());
     var mm = tmp.getMonth() + 1;
     if (mm < 10) mm = "0" + mm;
     var dd = tmp.getDate();
     if (dd < 10) dd = "0" + dd;
     return mm + "/" + dd + "/" + tmp.getFullYear();
    };
    
    0 讨论(0)
  • 2020-11-22 03:21

    I wrapped the correct answer of this question in a function that can add multiple leading zero's but defaults to adding 1 zero.

    function zeroFill(nr, depth){
      depth = (depth === undefined)? 1 : depth;
    
      var zero = "0";
      for (var i = 0; i < depth; ++i) {
        zero += "0";
      }
    
      return (zero + nr).slice(-(depth + 1));
    }
    

    for working with numbers only and not more than 2 digits, this is also an approach:

    function zeroFill(i) {
        return (i < 10 ? '0' : '') + i
      }
    
    0 讨论(0)
  • 2020-11-22 03:21

    The following aims to extract configuration, hook into Date.protoype and apply configuration.

    I've used an Array to store time chunks and when I push() this as a Date object, it returns me the length to iterate. When I'm done, I can use join on the return value.

    This seems to work pretty fast: 0.016ms

    // Date protoype
    Date.prototype.formatTime = function (options) {
        var i = 0,
            time = [],
            len = time.push(this.getHours(), this.getMinutes(), this.getSeconds());
    
        for (; i < len; i += 1) {
            var tick = time[i];
            time[i] = tick < 10 ? options.pad + tick : tick;
        }
    
        return time.join(options.separator);
    };
    
    // Setup output
    var cfg = {
        fieldClock: "#fieldClock",
        options: {
            pad: "0",
            separator: ":",
            tick: 1000
        }
    };
    
    // Define functionality
    function startTime() {
        var clock = $(cfg.fieldClock),
            now = new Date().formatTime(cfg.options);
    
        clock.val(now);
        setTimeout(startTime, cfg.options.tick);
    }
    
    // Run once
    startTime();
    

    demo: http://jsfiddle.net/tive/U4MZ3/

    0 讨论(0)
  • 2020-11-22 03:22

    Adding on to @modiX answer, this is what works...DO NOT LEAVE THAT as empty

    today.toLocaleDateString("default", {year: "numeric", month: "2-digit", day: "2-digit"})
    
    0 讨论(0)
  • 2020-11-22 03:23
    var MyDate = new Date();
    var MyDateString = '';
    MyDate.setDate(MyDate.getDate());
    var tempoMonth = (MyDate.getMonth()+1);
    var tempoDate = (MyDate.getDate());
    if (tempoMonth < 10) tempoMonth = '0' + tempoMonth;
    if (tempoDate < 10) tempoDate = '0' + tempoDate;
    MyDateString = tempoDate + '/' + tempoMonth + '/' + MyDate.getFullYear();
    
    0 讨论(0)
提交回复
热议问题