ActionScript 3 AS3从脏时间戳字符串获取格式化时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3从脏时间戳字符串获取格式化时间相关的知识,希望对你有一定的参考价值。

var timestampFromBackend:String = "/Date(1310127448000+0100)/";
trace("timestampFromBackend: " + timestampFromBackend);
var formattedTime:String = formatedTimeFromTimestamp(timestampFromBackend);
trace("formattedTime: " + formattedTime);

function formatedTimeFromTimestamp(str:String):String {
	/*
	Might get passed something like this ...
	/Date(1310127448000+0100)/
	but it also works if you are passed ...
	1310127448000+0100	
	*/

	var openBracketIndex:int;
	var closeBracketIndex:int;
	
	if (str.indexOf("(") != -1) {
		openBracketIndex = str.indexOf("(");
	} else {
		openBracketIndex = -1;
	}
	if (str.indexOf(")") != -1) {
		closeBracketIndex = str.indexOf(")");
	} else {
		closeBracketIndex = str.length;
	}

	str = str.substring(openBracketIndex + 1, closeBracketIndex);
	var arr:Array = new Array();
	if (str.indexOf("+") != -1) {
		arr = str.split("+");
		str = arr[0];
	} else if (str.indexOf("-") != -1) {
		arr = str.split("-");
		str = arr[0];
	}
	
	var timestampDate:Date = new Date(Number(str));
	var hrs:String = String(timestampDate.getHours());
	if (hrs.length < 2) {
		hrs = "0" + hrs;
	}
	var mins:String = String(timestampDate.getMinutes());
	if (mins.length < 2) {
		mins = "0" + mins;
	}
	
	str = hrs + ":" + mins;
	return str;
}

// OUTPUT
// timestampFromBackend: /Date(1310127448000+0100)/
// formattedTime: 13:17

以上是关于ActionScript 3 AS3从脏时间戳字符串获取格式化时间的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3从字符串中删除非数字字符

ActionScript 3 AS3获取字符串中的最后一个字符

ActionScript 3 AS3删除选项卡和NewLine字符

ActionScript 3 AS3将数字转换为字符串

ActionScript 3 AS3字符串到类名getDefinitionByName

ActionScript 3 AS3从字符串中剥离空格