Jul*_*tes 401 javascript date date-format time-format leading-zero
我已经创建了这个脚本,以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();
Run Code Online (Sandbox Code Playgroud)
我需要通过将这些规则添加到脚本中,使日期和月份组件的日期显示为前导零.我似乎无法让它发挥作用.
if (MyDate.getMonth < 10)getMonth = '0' + getMonth;
Run Code Online (Sandbox Code Playgroud)
和
if (MyDate.getDate <10)get.Date = '0' + getDate;
Run Code Online (Sandbox Code Playgroud)
如果有人能告诉我将这些插入脚本的位置,我会非常感激.
use*_*716 1258
试试这个:http://jsfiddle.net/xA5B7/
var MyDate = new Date();
var MyDateString;
MyDate.setDate(MyDate.getDate() + 20);
MyDateString = ('0' + MyDate.getDate()).slice(-2) + '/'
+ ('0' + (MyDate.getMonth()+1)).slice(-2) + '/'
+ MyDate.getFullYear();
Run Code Online (Sandbox Code Playgroud)
编辑:
为了解释,.slice(-2)给我们字符串的最后两个字符.
所以无论如何,我们可以添加"0"到日期或月份,并且只要求最后两个,因为那些总是我们想要的两个.
所以如果MyDate.getMonth()返回9,它将是:
("0" + "9") // Giving us "09"
Run Code Online (Sandbox Code Playgroud)
所以添加.slice(-2)它给我们最后两个字符是:
("0" + "9").slice(-2)
"09"
Run Code Online (Sandbox Code Playgroud)
但如果MyDate.getMonth()返回10,它将是:
("0" + "10") // Giving us "010"
Run Code Online (Sandbox Code Playgroud)
所以添加.slice(-2)给我们最后两个字符,或者:
("0" + "10").slice(-2)
"10"
Run Code Online (Sandbox Code Playgroud)
Ale*_*oss 100
以下是使用自定义"pad"函数的Mozilla Developer Network上的Date对象文档的示例,无需扩展Javascript的Number原型.他们给出的便利功能是
function pad(n){return n<10 ? '0'+n : n}
Run Code Online (Sandbox Code Playgroud)
以下是它在上下文中使用.
/* 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
Run Code Online (Sandbox Code Playgroud)
joa*_*16v 44
你可以定义一个"str_pad"函数(如在php中):
function str_pad(n) {
return String("00" + n).slice(-2);
}
Run Code Online (Sandbox Code Playgroud)
mod*_*diX 34
如果要按语言本地化日期输出并需要前导零,则解决方案看起来不同:
var date = new Date(2018, 2, 1);
var result = date.toLocaleDateString("en-GB", { // you can skip the first argument
year: "numeric",
month: "2-digit",
day: "2-digit",
});
console.log(result);Run Code Online (Sandbox Code Playgroud)
此外,您也可以使用toLocaleDateString年份选项.
只要你知道语言环境并希望以可读的形式显示日期,我认为这是最干净的工作方式.
不幸的是,IE10及更低版本不支持toLocaleDateString参数.
Ami*_*IRI 21
"use strict"
const today = new Date()
const year = today.getFullYear()
const month = `${today.getMonth() + 1}`.padStart(2, 0)
const day = `${today.getDate()}`.padStart(2, 0)
const stringDate = [day, month, year].join("/") // 13/12/2017
Run Code Online (Sandbox Code Playgroud)
在目标中String.prototype.padStart(targetLength[, padString])添加尽可能多padString的String.prototype目标,以便目标的新长度为targetLength.
"use strict"
let month = "9"
month = month.padStart(2, 0) // "09"
let byte = "00000100"
byte = byte.padStart(8, 0) // "00000100"
Run Code Online (Sandbox Code Playgroud)
Jul*_*kov 12
现在您还可以利用String.prototype.padStart快速轻松地达到目标
String(new Date().getMonth() + 1).padStart(2, '0')
Run Code Online (Sandbox Code Playgroud)
可以在caniuse评估可用性
var date = new Date()
var year = date.getFullYear()
var month = String(date.getMonth() + 1).padStart(2, '0')
var day = String(date.getDate()).padStart(2, '0')
console.log('%s/%s/%s', month, day, year)
Run Code Online (Sandbox Code Playgroud)
查看
String(new Date().getMonth() + 1).padStart(2, '0')
Run Code Online (Sandbox Code Playgroud)
适用于旧浏览器的 Polyfill
String.prototype.padStart || Object.defineProperty(String.prototype, 'padStart', {
configurable : true,
writable : true,
value : function (targetLength, padString) {
'use strict'
/**
* String.prototype.padStart polyfill
* /sf/ask/252365011/
*/
targetLength = targetLength | 0
padString = arguments.length > 1 ? String(padString) : ' '
if (this.length < targetLength && padString.length) {
targetLength = targetLength - this.length
while (padString.length < targetLength) {
padString += padString
}
return padString.slice(0, targetLength) + this
} else {
return this
}
}
})
Run Code Online (Sandbox Code Playgroud)
ken*_*bec 11
Number.prototype.padZero= function(len){
var s= String(this), c= '0';
len= len || 2;
while(s.length < len) s= c + s;
return s;
}
Run Code Online (Sandbox Code Playgroud)
//正在使用:
(function(){
var myDate= new Date(), myDateString;
myDate.setDate(myDate.getDate()+10);
myDateString= [myDate.getDate().padZero(),
(myDate.getMonth()+1).padZero(),
myDate.getFullYear()].join('/');
alert(myDateString);
})()
/* value: (String)
09/09/2010
*/
Run Code Online (Sandbox Code Playgroud)
小智 7
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();
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以解决这个问题,slice在 JavaScript 中使用。
var d = new Date();
var datestring = d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) +"-"+("0" + d.getDate()).slice(-2);
Run Code Online (Sandbox Code Playgroud)
在datestring返回日期格式如您所愿:2019年9月1日
另一种方法是使用dateformat库:https : //github.com/felixge/node-dateformat
您可以使用三元运算符将日期格式化为“if”语句。
例如:
var MyDate = new Date();
MyDate.setDate(MyDate.getDate()+10);
var MyDateString = (MyDate.getDate() < 10 ? '0' + MyDate.getDate() : MyDate.getDate()) + '/' + ((d.getMonth()+1) < 10 ? '0' + (d.getMonth()+1) : (d.getMonth()+1)) + '/' + MyDate.getFullYear();
Run Code Online (Sandbox Code Playgroud)
所以
(MyDate.getDate() < 10 ? '0' + MyDate.getDate() : MyDate.getDate())
Run Code Online (Sandbox Code Playgroud)
将类似于 if 语句,其中如果 getDate() 返回小于 10 的值,则返回“0”+ 日期,否则如果大于 10,则返回日期(因为我们不需要添加前导0)。同月。
编辑:忘记了 getMonth 以 0 开头,所以添加了 +1 来解释它。当然,您也可以只说 d.getMonth() < 9 :,但我认为使用 +1 将有助于使其更容易理解。
小智 6
我找到了最简单的方法:
MyDateString.replace(/(^|\D)(\d)(?!\d)/g, '$10$2');
Run Code Online (Sandbox Code Playgroud)
会将前导零添加到所有孤独的一位数字