将非标准日期字符串转换为日期

时间:2016-09-03 08:29:47

标签: javascript php date type-conversion

我的电子表格包含以下格式的100k日期:

Thursday 29th of October 2015 01:06:21 PM

我需要将它们转换为可用的日期格式。 YYYY / MM / DD,或任何事情都可以。只要我能把它变成标准格式。

我查看了各种PHP / JS日期函数,但这个日期似乎没有得到认可。我认为可以用一些正则表达式完成,我想知道是否有人曾经来过这里?

4 个答案:

答案 0 :(得分:1)

您只需将'of '替换为空字符串,然后使用Date即可。



var dateString = 'Thursday 29th of October 2015 01:06:21 PM'.replace('of ', ''),
    date = new Date(dateString);

console.log(date);

    




答案 1 :(得分:0)

此代码肯定会对..

有所帮助
    var DateObj = new Date()

    function generateDateTOYYMMDD(DateObj){
        var DateDay = DateObj.getDate();
        DateDay = (DateDay<10) ? "0"+DateDay : DateDay;
        var DateMonth = DateObj.getMonth() + 1;
        DateMonth = (DateMonth<10) ? "0"+DateMonth : DateMonth;
        var DateYear = DateObj.getFullYear();
        var DateStr = DateYear + "-" + DateMonth+ "-" + DateDay;
        return DateStr;
    }

答案 2 :(得分:0)

您可以使用string.split()和array.indedOf(); 一些例子。

&#13;
&#13;
var dateString = "Thursday 29th of October 2015 01:06:21 PM";
var parts = [];
parts = dateString.split(" ");
console.log(parts);
&#13;
&#13;
&#13;

&#13;
&#13;
var months = ["January", "February","March"];
var monthInt = months.indexOf("March");
console.log(monthInt);
&#13;
&#13;
&#13;

之后你只需转换为日期

&#13;
&#13;
var newDate = new Date();
newDate.setMonth(3);
newDate.setDate(15);
newDate.setYear(2016);
console.log(newDate)
&#13;
&#13;
&#13;

答案 3 :(得分:0)

在PHP中,DateTime :: createFromFormat - 返回根据指定格式格式化的新DateTime对象

  $str_date = 'Thursday 29th of October 2015 01:06:21 PM';
    $obj_date = DateTime::createFromFormat('l jS \of F Y h:i:s A', $str_date);
    echo date('Y/m/d',$obj_date->getTimestamp());