我已经创建了这个脚本,以dd/mm/yyyy的格式提前计算10天的日期:

var MyDate = new Date();
var MyDateString = new Date();
MyDate.setDate(MyDate.getDate()+10);
MyDateString = MyDate.getDate() + '/' + (MyDate.getMonth()+1) + '/' + MyDate.getFullYear();

我需要通过将这些规则添加到脚本中,使日期在日期和月份组件上以前导零出现。我好像不能让它工作。

if (MyDate.getMonth < 10)getMonth = '0' + getMonth;

and

if (MyDate.getDate <10)get.Date = '0' + getDate;

如果有人能告诉我在哪里插入这些脚本,我会非常感激。


当前回答

我将这个问题的正确答案包装在一个函数中,该函数可以添加多个前导零,但默认为添加1个零。

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));
}

对于只处理数字且不超过2位数的情况,这也是一种方法:

function zeroFill(i) {
    return (i < 10 ? '0' : '') + i
  }

其他回答

toISOString可以得到前导0

const currentdate = new Date(); const date = new date (date. utc (currentdate.getFullYear(), (currentdate.getMonth()),currentdate.getDate(), currentdate.getHours(), currentdate.getMinutes(), currentdate.getSeconds())); //你可以传递YY, MM, DD //op: 2018-03-01 //i have passed YY, MM, DD, HH, Min, Sec // op: 2021-06-09T12:14:27.000Z console.log (date.toISOString ());

输出将类似于此:2021-06-09T12:14:27.000Z

这里有一个非常简单的例子,你可以如何处理这种情况。

var mydate = new Date(); var month = (mydate.getMonth(). tostring()。长度< 2 ?“0”+ mydate.getMonth () .toString (): mydate.getMonth ()); var date = (mydate.getDate(). tostring()。长度< 2 ?“0”+ mydate.getDate () .toString (): mydate.getDate ()); var year = mydate.getFullYear(); console.log(“格式Y-m-d:”+“-”+月+“-”+日期); Y / m / d: console.log(“格式”,年+ " +月+ " +日期);

下面是一个来自Mozilla Developer Network的Date对象文档的例子,它使用了一个自定义的“pad”函数,而不需要扩展Javascript的Number原型。他们给出的一个方便的例子是

function pad(n){return n<10 ? '0'+n : n}

下面是它在上下文中的用法。

/* use a function for the exact format desired... */
function ISODateString(d){
    function pad(n){return n<10 ? '0'+n : n}
    return d.getUTCFullYear()+'-'
    + pad(d.getUTCMonth()+1)+'-'
    + pad(d.getUTCDate())+'T'
    + pad(d.getUTCHours())+':'
    + pad(d.getUTCMinutes())+':'
    + pad(d.getUTCSeconds())+'Z'
}

var d = new Date();
console.log(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z

另一种选择,使用内置函数来填充(但会导致相当长的代码!):

myDateString = myDate.getDate().toLocaleString('en-US', {minimumIntegerDigits: 2})
  + '/' + (myDate.getMonth()+1).toLocaleString('en-US', {minimumIntegerDigits: 2})
  + '/' + myDate.getFullYear();

// '12/06/2017'

另一个,用正则表达式操作字符串:

var myDateString = myDate.toISOString().replace(/T.*/, '').replace(/-/g, '/');

// '2017/06/12'

但是要注意的是,在开头显示年份,在结尾显示日期。

下面的目的是提取配置,挂钩到日期。原型和应用配置。

我已经使用了一个数组来存储时间块,当我将()它作为一个Date对象时,它返回我要迭代的长度。完成后,我可以对返回值使用join。

这似乎工作得相当快:0.016毫秒

// 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();

演示:http://jsfiddle.net/tive/U4MZ3/