【问题标题】:Convert UTC Date to datetime string Titanium将 UTC 日期转换为日期时间字符串 Titanium
【发布时间】:2012-11-14 13:47:30
【问题描述】:

我有一个日期字符串 "2012-11-14T06:57:36+0000" 我想将其转换为以下格式 "Nov 14 2012 12:27"强>。我尝试了很多解决方案,包括Convert UTC Date to datetime string Javascript。但没有什么能帮助我。以下代码在 android 中对我有用。但对于 ios,它显示为 invalid date

var date = "2012-11-14T06:57:36+0000";
//Calling the function
date = FormatDate(date);

//Function to format the date
function FormatDate(date)
{   
    var newDate = new Date(date);
    newDate = newDate.toString("MMMM");
    return (newDate.substring(4,21));   
}

谁能帮助我?提前致谢

【问题讨论】:

    标签: javascript titanium titanium-mobile datetime-format


    【解决方案1】:

    并非所有浏览器都支持相同的日期格式。我们可以选择的最佳方法是在分隔符 - 和 : 上拆分字符串,并将每个结果数组项传递给 Date 构造函数,请参见以下函数

    function FormatDate(date)
    {   
        var arr = date.split(/[- :T]/), // from your example var date = "2012-11-14T06:57:36+0000";
        date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], 00);
        newDate = date.toString("MMMM");
        //.. do further stuff here  
    }
    

    【讨论】:

      【解决方案2】:

      你可以通过初始化一个新的日期得到一个Date对象:

      var date = "2012-11-14T06:57:36+0000";
      var newDate = new Date(date); // this will parse the format
      
      console.log(newDate);
      > Wed Nov 14 2012 01:57:36 GMT-0500 (EST)
      

      至于格式,已经有几个线程(如this one)。

      还有一些用于日期格式化和处理的库,人们在进行大量日期处理时通常最终会使用这些库。如果您正在寻找类似的东西,我会建议 Datejs

      【讨论】:

        猜你喜欢
        • 2021-04-19
        • 1970-01-01
        • 2015-10-24
        • 2011-06-13
        • 1970-01-01
        • 2014-11-18
        • 2014-02-22
        • 1970-01-01
        • 2020-03-21
        相关资源
        最近更新 更多