في كثير من الأحيان ، يمكننا استخدام أساليب الكائنات المدمجة في جافا سكريبت لتنسيقها ، مثل:
نسخة الكود كما يلي: var d = New Date () ؛
Console.log (D) ؛
Console.log (D.TodateString ()) ؛
Console.log (D.Togmtstring ()) ؛
Console.log (D.ToisoString ()) ؛
Console.log (D.Tojson ()) ؛
console.log (D.TolocaledateString ()) ؛
console.log (D.Tolocalestring ()) ؛
console.log (D.Tolocaletimestring ()) ؛
Console.log (D.ToString ()) ؛
console.log (D.Totimestring ()) ؛
Console.log (D.ToutcString ()) ؛
إذا لم تتمكن الطريقة أعلاه من تلبية متطلباتنا ، فيمكنك أيضًا تخصيص الوظيفة لتنسيق الوقت ، مثل:
نسخة الكود كما يلي:
date.prototype.format = function (format) {
تاريخ var = {
"M +": this.getMonth () + 1 ،
"D+": this.getDate () ،
"H+": this.gethours () ،
"M+": this.getMinutes () ،
"S+": this.getSeconds () ،
"Q +": Math.Floor ((this.getMonth () + 3) / 3) ،
"S+": this.getMillisEconds ()
} ؛
if (/(y+)/i.test(format)) {
format = format.replace (regexp. $ 1 ، (this.getlyear () + '') .Substr (4 - regexp. $ 1.Length)) ؛
}
لـ (var k في التاريخ) {
if (new regexp ("(" + k + ")"). اختبار (تنسيق)) {
format = format.replace (regexp. $ 1 ، regexp. $ 1.Length == 1
تاريخ [k]: ("00" + تاريخ [k]).
}
}
تنسيق العودة
}
var d = new Date (). format ('Yyyy-MM-DD') ؛
Console.log (D) ؛