JavaScript日期时间不工作

JavaScript DateTime not working

本文关键字:工作 时间 日期 JavaScript      更新时间:2023-09-26
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中工作。因此,为了使这段代码在所有浏览器上都能正常工作,不应该使用它。

以下代码适用于FirefoxChrome:

我已经用/替换了字符串变量日期-。此格式适用于Firefox和Chrome。

另一个在FirefoxChrome中工作的格式是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'));