如何在JavaScript中获取当前日期?
当前回答
你可以使用我下面给出的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;
} ());
其他回答
您可以签出此
var today = new Date();
today = parseInt(today.getMonth()+1)+'/'+today.getDate()+'/'+today.getFullYear()+"\nTime : "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
document.write(today);
请参阅Date()构造函数的文档。链接
在React js中获取当前日期月年
您可以使用扩展Date对象的Date.js库,因此可以使用.today()方法。
更清洁、更简单的版本:
new Date().toLocaleString();
结果因用户的语言环境而异:
2017年2月27日上午9:15:41
Varun的答案不考虑TimezoneOffset。以下是一个版本:
var d = new Date()
new Date(d.getTime() - d.getTimezoneOffset() * 60000).toJSON().slice(0, 10) // 2015-08-11
TimezoneOffset是分钟,而Date构造函数需要毫秒,因此乘以60000。
var d=(new Date()).toString().split('').spling(1,3).join('');文档.写入(d)
要将其分解为步骤:
(new Date()).toString()给出“2013年6月28日星期五15:30:18 GMT-0700(PDT)”(new Date()).toString().split(“”)将上述字符串除以每个空格,并返回一个数组,如下所示:[“Fri”,“Jun”,”28“,”2013“,”15:31:14“,”GMT-0700“,”(PDT)“](new Date()).toString().split('').spling(1,3).join('')从上述数组中获取第二、第三和第四个值,用空格将它们连接起来,并返回字符串“Jun 28 2013”
推荐文章
- 给一个数字加上st, nd, rd和th(序数)后缀
- 如何以编程方式触发引导模式?
- setTimeout带引号和不带括号的区别
- 在JS的Chrome CPU配置文件中,'self'和'total'之间的差异
- 用javascript检查输入字符串中是否包含数字
- 如何使用JavaScript分割逗号分隔字符串?
- 在Javascript中~~(“双波浪号”)做什么?
- 谷歌chrome扩展::console.log()从后台页面?
- 未捕获的SyntaxError:
- [].slice的解释。调用javascript?
- jQuery日期/时间选择器
- 我如何预填充一个jQuery Datepicker文本框与今天的日期?
- 数组的indexOf函数和findIndex函数的区别
- jQuery添加必要的输入字段
- Access-Control-Allow-Origin不允许Origin < Origin >