gpt4 book ai didi

php - as3 - 格式化来自 mysql 的时间戳字符串

转载 作者:行者123 更新时间:2023-11-29 03:59:15 25 4
gpt4 key购买 nike

我从一个 php 文件中接收到一个日期字段,我需要对其进行格式化。我得到这个字符串:“2009-11-12 17:58:13”我想转换为“2009 年 11 月 12 日下午 5:58:13”

我尝试使用 Date 类,但我得到的是这样的东西:类型强制失败:无法将“2009-11-12 17:58:13”转换为日期。

有人知道这样做有什么好的实用程序吗?

最佳答案

如果没有直接的解决方案,您可以求助于正则表达式:

private function formatDate(str:String):String
{

var regex:RegExp = /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/;
var matches:Object = regex.exec(str);
var date:Date = new Date(matches[1], Number(matches[2]) - 1, matches[3],
matches[4], matches[5], matches[6]);
var months:Array = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];
var result:String = months[date.month] + " ";
result += date.date + ", " + date.fullYear + " ";
if(date.hours > 12)
result += (date.hours - 12) + ":" + date.minutes + ":" +
date.seconds + " pm";
else
result += date.hours + ":" + date.minutes + ":" + date.seconds + " am";
return result;
}

正则表达式解释 /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/

// Forward slashes - / - at the beginning and end are delimiters

\d+ /* One or more number of digits.
* Ideally it should be \d{n} (\d{4} - 4 digits),
* but I'm assuming that the input string would always be
* valid and properly formatted
*/

- and : //A literal hyphen/colon

\s //A whitespace (use \s+ to skip extra whitespaces)

() /* Parenthetic groups used to capture a string for using it later
* The return value of regex.exec is an array whose first element is
* the complete matched string and subsequent elements are contents
* of the parenthetic groups in the order they appear in the regex
*/

关于php - as3 - 格式化来自 mysql 的时间戳字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1726586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com