JavaScript日期时间不工作
JavaScript DateTime not working
function formatDate(dt) {
//var date = new Date(dt);
var date = new Date('2015-08-27 16:00:00'); alert(date.getMonth());
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return date.getDate() + " " + date.getMonth() + " " + date.getFullYear() + " " + strTime;
}
我试图获取日期和时间。但我得到的是NaN
而不是date.getMonth();
。如果我删除时间,那么这是工作很好。但是我的日期时间格式是动态的。这是来自数据库,如0000-00-00 00:00:00
。
我想以27 Aug 2015 04:00:00 am/pm
格式查看我的数据库日期和时间。
您使用的日期格式(2015-08-27 16:00:00
)不是Firefox的正确格式,尽管它可以在Chrome中工作。因此,为了使这段代码在所有浏览器上都能正常工作,不应该使用它。
我已经用/
替换了字符串变量日期-
。此格式适用于Firefox和Chrome。
另一个在Firefox和Chrome中工作的格式是1995-12-17T03:24:00
,其中包括T
而不是' '
(space)。
但是,上述格式在Chrome和Firefox中给出不同的值。
new Date('2015-10-05T03:24:00'); // Returns Mon Oct 05 2015 08:54:00 GMT+0530 (IST) in Chrome
new Date('2015-10-05T03:24:00'); // Returns 2015-10-04T21:54:00.000Z in Firefox
var date1 = '2015-08-20 09:38:20';
var date1Updated = new Date(date1.replace(/-/g,'/'));
alert(date1Updated.getMonth());
var strDate = addZero(d.getDate()) + "/" + addZero((d.getMonth() + 1))+"/" +d.getFullYear();
alert("strDate :"+strDate)
return strDate;
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
getMonth()
方法根据当地时间返回指定日期的月份(从0到11)。
注释: 1月为0,2月为1,以此类推。
你需要添加一个像getMonth() + 1
。
function formatDate(dt) {
//var date = new Date(dt);
var date = new Date('2015-08-27 16:00:00');
//alert(date.getMonth() + 1);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return date.getDate() + " " + (date.getMonth() + 1) + " " + date.getFullYear() + " " + strTime;
}
alert(formatDate());
在firefox中'2015-08-27 16:00:00'
是一个无效的日期。
你的选项是
var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);
在你的情况下,你错过了T
之前的时间
您有一个无效的日期格式,似乎Chrome处理这种情况,但firefox没有。
new Date('2015-08-27 16:00:00') // Invalid format
new Date('2015-08-27T16:00:00') // Correct Format With T
你的代码不能在firefox中运行
在firefox中2015-08-27 16:00:00
是无效的。为了有效,它必须是2015-08-27T16:00:00
。要使它在firefox中有效,最简单的解决方案是
function formatDate(dt) {
//var date = new Date(dt);
var sampleDate = "2015-08-27 16:00:00"; // Your sample date as string
var another = sampleDate.replace(' ', 'T');// Change here. Replaced the empty space with the 'T' to make it work in firefox
var date = new Date(another); alert(date.getMonth()); // Using your date to create new Date object
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return date.getDate() + " " + date.getMonth() + " " + date.getFullYear() + " " + strTime;
}
希望对大家有所帮助
使用Date构造函数解析字符串很大程度上依赖于实现。只有一种字符串格式被指定为规范所支持,并且在ES5和ECMAScript 2015之间发生了一定程度的变化。
最好的选择是手动解析字符串,使用自己的函数或库。如果字符串与OP中的格式一致,且时区为UTC,则适用以下格式:
/* parse dates of format yyyy-mm-dd hh:mm:ss
** e.g. 2015-08-27 16:00:00
**
** @param {string} s - Date string in format yyyy-mm-dd hh:mm:ss
** @returns {Date} - String as Date assuming UTC
**
** Does not validate that the string is valid date or time
**/
function parseDate (s) {
var b = s.split(/'D/);
return new Date(Date.UTC(b[0], b[1]-1, b[2], b[3], b[4], b[5]));
}
document.write(parseDate('2015-08-27 16:00:00'));
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- clearInterval停止setInterval纯粹基于时间工作
- Hammer.js过了一段时间就停止工作了
- 当页面回发ASP.NET时,时间选择器javascript将停止工作
- 在当天的时间选取器中禁用时间无法正常工作
- javascript使jquery倒计时与UTC时间一起工作
- 关闭网页时没有显示 Js 的工作时间
- 检查工作时间
- 检查工作时间
- 工作时间脚本 javascript
- 工作时间为25天的节点的调度器
- 用JavaScript计算2个不同时间表的工作时间
- 如何计算两次之间的工作时间
- PHP会话问题,不是100%的工作时间
- 零位板的工作时间有一半
- 用于分析工作时间字符串的RegEx模式
- 从工作时间时间范围内减去午餐时间
- Javascript多个工作时间计算从日期到现在
- 不方便的工作时间mysql之间的报告时间
- 每天两班制的多个工作时间