Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 格式化输出日期_Javascript_Facebook - Fatal编程技术网

Javascript 格式化输出日期

Javascript 格式化输出日期,javascript,facebook,Javascript,Facebook,如何使用Javascript格式化日期字符串。例如: var date_str = "2010-10-06T03:39:41+0000"; 结果应该是这样的: 11:39 AM Oct 6th 对此有什么想法吗?注:date_str是Facebook Graph API返回日期的一个示例 提前感谢。从phpjs.org使用此函数:Date.parse可以解析此函数,如果您剥离+0000部分。然后,您可以在日期对象上使用setTime,您可以自己打印,也可以使用一些库。解析日期应该不会太困难-

如何使用Javascript格式化日期字符串。例如:

var date_str = "2010-10-06T03:39:41+0000";
结果应该是这样的:

11:39 AM Oct 6th
对此有什么想法吗?注:date_str是Facebook Graph API返回日期的一个示例


提前感谢。

从phpjs.org使用此函数:

Date.parse可以解析此函数,如果您剥离+0000部分。然后,您可以在日期对象上使用setTime,您可以自己打印,也可以使用一些库。

解析日期应该不会太困难-所有组件的顺序都是正确的,因此您只需在特殊字符上拆分,从月值中减去1,然后将数组减去最后一个元素应用到Date.UTC():

这为我们提供了一个指定日期的日期对象,其余的只是将每个组件重新格式化为字符串:

    // Declare an array of short month names
    var mon  = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov", "Dec"];
    // Determine between AM and PM
        ampm = date.getHours() > 12 ? "PM" : "AM",

    // Prepare for working out the date suffix
        nth  = date.getDate();

    // Date is nth for 11th, 12th and 13th
    if (nth >= 11 && nth <= 13)
        nth = "th";
    // Otherwise it's st, nd, rd, th if the date ends in 1, 2, 3 or anything else
    else
        nth  = ["", "st", "nd", "rd"][(nth+"").slice(-1)] || "th";

    // Return the string formatted date
    return (date.getHours() % 12) + ":" + (date.getMinutes()) + " " + ampm +
           " " + mon[date.getMonth()] + " " + date.getDate() + nth;
}

// Example:
parseDate("2010-10-06T03:39:41+0000");
// -> "4:39 AM Oct 6th" (in my timezone)
//声明一个短月份名称数组
变量mon=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
//确定上午和下午之间的时间
ampm=date.getHours()>12?“PM”:“AM”,
//准备计算日期后缀
nth=date.getDate();
//日期是第11、12和13号

如果(第n个>=11&&n个日期(格式、时间戳)可以,但是问题是如何把DATEYSTR转换成时间戳,这样我就可以把它传递给函数。在T代码的中间有什么意义?> DATEYSTR < /代码>?我假设它与我们如何从03:39到11:39……我想是……它只是从脸谱网API……ErnNOCO返回的字符串:这是ISO8601日期,<代码> + 0000 < /代码>描述了时区偏移距UTC为+0。Facebook可能只是在@Trez的本地时区中将此日期显示为上午11:39(+8小时)。@Trez:我注意到日期后缀部分有一个小错误,我现在已经修复了。希望您在继续实施解决方案之前能够看到此错误:-)
    // Declare an array of short month names
    var mon  = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov", "Dec"];
    // Determine between AM and PM
        ampm = date.getHours() > 12 ? "PM" : "AM",

    // Prepare for working out the date suffix
        nth  = date.getDate();

    // Date is nth for 11th, 12th and 13th
    if (nth >= 11 && nth <= 13)
        nth = "th";
    // Otherwise it's st, nd, rd, th if the date ends in 1, 2, 3 or anything else
    else
        nth  = ["", "st", "nd", "rd"][(nth+"").slice(-1)] || "th";

    // Return the string formatted date
    return (date.getHours() % 12) + ":" + (date.getMinutes()) + " " + ampm +
           " " + mon[date.getMonth()] + " " + date.getDate() + nth;
}

// Example:
parseDate("2010-10-06T03:39:41+0000");
// -> "4:39 AM Oct 6th" (in my timezone)