如何在JavaScript中获取当前日期?
当前回答
在澳大利亚,我更喜欢使用此
(new Date()).toISOString().slice(0, 10).split("-").reverse().join("/")
其他回答
这有什么大不了的。。最干净的方法是
var currentDate=新日期().toLocaleString().slice(0,10);
你可以使用我下面给出的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;
} ());
(function() { var d = new Date(); return new Date(d - d % 86400000); })()
像这样漂亮地打印日期。
2015年6月1日上午11:36:48
https://gist.github.com/Gerst20051/7d72693f722bbb0f6b58
var dateTimeToday = new Date();
var dateToday = new Date(
dateTimeToday.getFullYear(),
(dateTimeToday.getMonth() + 1) /*Jan = 0! */,
dateTimeToday.getDate(),
0,
0,
0,
0);
推荐文章
- 我如何检查如果一个变量是JavaScript字符串?
- 如何检测如果多个键被按下一次使用JavaScript?
- MySQL中两个日期之间的差异
- 如何通过history. pushstate获得历史变化的通知?
- 在Bash中获取日期(比当前时间早一天)
- 使用jQuery改变输入字段的类型
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 我怎么能计算在打字稿2日期之间的时间