如何在JavaScript中获取当前日期?
当前回答
这可能会帮助你
var date = new Date();
console.log(date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getFullYear());
这将以dd/MM/yyyy格式打印当前日期
其他回答
您可以使用扩展Date对象的Date.js库,因此可以使用.today()方法。
像这样漂亮地打印日期。
2015年6月1日上午11:36:48
https://gist.github.com/Gerst20051/7d72693f722bbb0f6b58
大多数其他答案都提供了日期和时间。如果你只需要日期。
new Date().toISOString().split("T")[0]
输出
[ '2021-02-08', '06:07:44.629Z' ]
如果要使用/format,请使用replaceAll。
new Date().toISOString().split("T")[0].replaceAll("-", "/")
如果您需要其他格式,那么最好使用momentjs。
基础知识
如果您对2016年1月24日星期日21:23:07 GMT+0100(CET)格式感到满意,您可以使用以下代码:
var today = new Date();
Date.product.toLocaleDateString()
如果要格式化输出,请考虑使用Date.protype.toLocaleDateString():
var today = new Date().toLocaleDateString('de-DE', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
如果您今天(1月24日)执行了该代码ᵗʰ,2016),它将产生字符串Sonntag,24。2016年1月。但是,较旧的浏览器可能会产生不同的结果,例如IE<11不支持区域设置或选项参数。
正在自定义
如果Date.product.toLocaleDateString()不够灵活,无法满足您可能需要的任何需求,您可能需要考虑创建一个自定义Date对象,如下所示:
var DateObject = (function() {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var date = function(str) {
this.set(str);
};
date.prototype = {
set : function(str) {
var dateDef = str ? new Date(str) : new Date();
this.day = dateDef.getDate();
this.dayPadded = (this.day < 10) ? ("0" + this.day) : "" + this.day;
this.month = dateDef.getMonth() + 1;
this.monthPadded = (this.month < 10) ? ("0" + this.month) : "" + this.month;
this.monthName = monthNames[this.month - 1];
this.year = dateDef.getFullYear();
}
};
return date;
})();
如果您今天(1月24日)包含了该代码并执行了新的DateObject()ᵗʰ,2016),它将生成具有以下财产的对象:
day: 24
dayPadded: "24"
month: 1
monthPadded: "01"
monthName: "January"
year: 2016
你可以使用我下面给出的DATE API,每天使用日期格式以及获取当前日期、昨天等。如何使用例如
var dt = new Date();
/// ANY DATE YOU WANT -- dt = new Date(""July 21, 1983 01:15:00"")
dateObj = dt.getFormattedDate();
alert( dateObj.isToday() );
alert( dateObj.todayDay() );
alert( dateObj.monthNameDayYear() );
(function () {
fnDateProcessor = function () {
var that = this;
return {
yyyymmdd: function (separator) {
var fdate = this.formatDate(true, true) ,
separator = separator ? separator : "-";
return fdate.year + separator + fdate.month + separator + fdate.day;
},
monthNameDayYear: function () {
var fdate = this.formatDate(true, true);
return fdate.monthName + " " + fdate.day + ", " + fdate.year;
},
ddmmyyyy: function (separator) {
var fdate = this.formatDate(true, true) ,
separator = separator ? separator : "/";
return fdate.day + separator + fdate.month + separator + fdate.year;
},
meridianTime: function () {
var fdate = this.formatDate();
return fdate.hour + ":" + fdate.minute + " " + fdate.meridian;
},
monthDay: function (separator) {
var fdate = this.formatDate();
separator = checkSeparator(separator);
return fdate.monthName.substring(0, 3) + separator + fdate.day;
},
weekMonthDayYear: function () {
var fdate = this.formatDate();
//separator = checkSeparator(separator);
return fdate.weekDay + " " + fdate.monthName.substring(0, 3) +
fdate.day + " ," + fdate.year;
},
timeZoneInclusive: function () {
return new Date(that);
},
todayDay: function () { return new Date().getDate(); },
todayMonth: function () { return new Date().getMonth() + 1; },
dateDay: function () { return this.formatDate().day; },
dateMonth: function () { return this.formatDate().month; },
isToday: function () { return this.sameDate(new Date()); },
isYesterday: function () {
d = new Date(); d.setDate(d.getDate() - 1);
return this.sameDate(d);
},
formatDate: function () {
var zeroPaddedMnth = true, zeroPaddedDay = false,
zeroPaddedHr = false, zeroPaddedMin = true;
// Possible to take Options arg that overide / merge to defaults
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var day = getFormattedDay(that.getDate(), zeroPaddedDay);
var monthIndex = that.getMonth();
var month = getFormattedMonth(monthIndex + 1, zeroPaddedMnth);
var year = that.getFullYear();
var wkDay = that.getDay();
var hour = getFormattedHour(that.getHours(), zeroPaddedHr);
var minute = getFormattedMinute(that.getMinutes(), zeroPaddedMin);
var meridian = getMeridian(that.getHours());
return {
"day": day, "monthName": monthNames[monthIndex], "month": month,
"weekDay": weekDays[wkDay], "year": year, "hour": hour, "minute": minute,
"meridian": meridian
};
},
compareDate: function (d2) { /// validates if caller is less than argument
d2 = _isString(d2) ? new Date(d2) : d2;
return !this.sameDate(d2)
&& typeof d2 != "number"
? that < d2 : false;
},
sameDate: function (d) {
return that.getFullYear() === d.getFullYear()
&& that.getDate() === d.getDate()
&& that.getMonth() === d.getMonth();
},
dateAfter: function (separator) {
var fdate = this.formatDate();
var separator = separator ? separator : "-";
return fdate.year + separator + fdate.month + separator + (fdate.day + 1);
}
};
};
function _isString(obj) {
var toString = Object.prototype.toString;
return toString.call(obj) == '[object String]';
}
function checkSeparator(separator) {
// NOT GENERIC ... NEEDS REVISION
switch (separator) {
case " ": sep = separator; break;
case ",": sep = " ,"; break;
default:
sep = " "; break;
}
return sep;
}
function getFormattedHour(h, zeroPadded) {
h = h % 12;
h = h ? h : 12; // 12 instead of 00
return zeroPadded ? addZero(h) : h;
}
function getFormattedMinute(m, zeroPadded) {
return zeroPadded ? addZero(m) : m;
}
function getFormattedDay(dd, zeroPadded) {
return zeroPadded ? addZero(dd) : dd;
}
function getFormattedMonth(mm, zeroPadded) {
return zeroPadded ? addZero(mm) : mm;
}
function getMeridian(hr) {
return hr >= 12 ? 'PM' : 'AM';
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
Date.prototype.getFormattedDate = fnDateProcessor;
} ());
推荐文章
- 我如何检查如果一个变量是JavaScript字符串?
- 如何检测如果多个键被按下一次使用JavaScript?
- MySQL中两个日期之间的差异
- 如何通过history. pushstate获得历史变化的通知?
- 在Bash中获取日期(比当前时间早一天)
- 使用jQuery改变输入字段的类型
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 我怎么能计算在打字稿2日期之间的时间