在javascript中将日期从'2011年6月9日00:00:00 GMT + 0530(印度标准时间)'转换为'YYYY-MM-DD'

时间:2011-06-09 10:14:09

标签: javascript

如何将时间从格式“2011年6月9日00:00:00 GMT + 0530(印度标准时间)”转换为YYYY-MM-DD。

当我尝试提醒()日期时,这将显示如下日期 - 2011年6月9日星期四00:00:00 GMT + 0530(印度标准时间)

但我需要YYYY-MM-DD格式的时间。

是否有要转换的内置函数?

6 个答案:

答案 0 :(得分:19)

您可以使用Date构造函数解析日期,然后吐出各个时间组件:

function convert(str) {
    var date = new Date(str),
        mnth = ("0" + (date.getMonth()+1)).slice(-2),
        day  = ("0" + date.getDate()).slice(-2);
    return [ date.getFullYear(), mnth, day ].join("-");
}

convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)");
//-> "2011-06-08"

从结果中可以看出,这会将日期解析为本地时区。如果您想根据原始时区保留日期,最简单的方法是拆分字符串并提取您需要的部分:

function convert(str) {
    var mnths = { 
        Jan:"01", Feb:"02", Mar:"03", Apr:"04", May:"05", Jun:"06",
        Jul:"07", Aug:"08", Sep:"09", Oct:"10", Nov:"11", Dec:"12"
    },
    date = str.split(" ");

    return [ date[3], mnths[date[1]], date[2] ].join("-");
}

convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)");
//-> "2011-06-09"

答案 1 :(得分:3)

function convert(str) {
    var date = new Date(str),
        mnth = ("0" + (date.getMonth()+1)).slice(-2),
        day  = ("0" + date.getDate()).slice(-2);
        hours  = ("0" + date.getHours()).slice(-2);
        minutes = ("0" + date.getMinutes()).slice(-2);
    return [ date.getFullYear(), mnth, day, hours, minutes ].join("-");
}

我在角度中有效地使用了这个,因为我在更新$ scope.START事件时失去了两个小时,并且$ scope.ENDevent,IN console.log很好,但是保存到mYsql会减少两个小时。

var whatSTART = $scope.STARTevent;
whatSTART = convert(whatever);

这也适用于END

答案 2 :(得分:3)

对我来说,转换日期最简单的方法是将日期字符串化然后切成薄片。

var event = new Date("Fri Apr 05 2019 16:59:00 GMT-0700 (Pacific Daylight Time)");

let date = JSON.stringify(event)
date = date.slice(1,11)

// console.log(date) = '2019-04-05'

答案 3 :(得分:1)

以上解决方案仅在字符串时才有效。输入类型日期,在某些情况下为javascript 日期对象提供输出,例如使用angular等。这就是为什么有些人会收到像#34; TypeError:str.split不是函数"的错误。它是一个日期对象,所以你应该在javascript中使用Date对象的函数来操作它。 示例:

var date = $scope.dateObj ; 
//dateObj is data bind to the ng-modal of input type dat.
console.log(date.getFullYear()); //this will give you full year eg : 1990
console.log(date.getDate()); //gives you the date from 1 to 31
console.log(date.getMonth() + 1); //getMonth will give month from 0 to 11 

请查看以下链接以供参考:

答案 4 :(得分:0)

 function convertDatePickerTimeToMySQLTime(str) {
        var month, day, year, hours, minutes, seconds;
        var date = new Date(str),
            month = ("0" + (date.getMonth() + 1)).slice(-2),
            day = ("0" + date.getDate()).slice(-2);
        hours = ("0" + date.getHours()).slice(-2);
        minutes = ("0" + date.getMinutes()).slice(-2);
        seconds = ("0" + date.getSeconds()).slice(-2);

        var mySQLDate = [date.getFullYear(), month, day].join("-");
        var mySQLTime = [hours, minutes, seconds].join(":");
        return [mySQLDate, mySQLTime].join(" ");
    }

答案 5 :(得分:0)

var first_date = new Date("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"); var start_date = first_date.toLocaleDateString().slice(0,10);

相关问题