将UTC日期转换为日期时间字符串Titanium [英] Convert UTC Date to datetime string Titanium

查看:119
本文介绍了将UTC日期转换为日期时间字符串Titanium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串2012-11-14T06:57:36 + 0000,我想将其转换为以下格式2012年11月14日12:27。我尝试了很多解决方案,包括将UTC日期转换为日期时间字符串Javascript 。但没有什么可以帮助我。以下代码在android中为我工作。但对于ios,它显示为无效日期

I have a date string "2012-11-14T06:57:36+0000" that I want to convert to the following format "Nov 14 2012 12:27". I have tried a lot of solutions including Convert UTC Date to datetime string Javascript. But nothing could help me. The following code worked for me in android. But for ios it displays as 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));   
}

任何人都可以帮助我吗?提前致谢

Can anyone help me? Thanks in advance

推荐答案

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

All browsers doesn't support the same date formats. The best approach we can choose is to split the string on the separator characters -, and : , and pass each of the resulting array items to the Date constructor, see the following function

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  
}

这篇关于将UTC日期转换为日期时间字符串Titanium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆