AS3 to convert a string to a date time object

Source: Internet
Author: User
Tags string format tostring


No scheme:

var datestr:string = "2013-8-7 9:30:15";
var date:date = new Date ();
Date.time = Date.parse (DATESTR);//no
Trace (date.tostring ());


Why no:

As in the code, the Date.pare fails because the string format is required for parse

Several formats listed on the document:

     mm/dd/yyyy HH:MM:SS TZD
     HH:MM:SS TZD Day mon/dd/yyyy 
     Mon dd YYYY HH:MM:SS TZD Day
     Mon dd HH:MM:SS TZD yyyy
  
   day DD Mon HH:MM:SS TZD YYYY
     mon/dd/yyyy HH:MM:SS TZD yyyy/mm/dd
     HH:MM:SS TZD
  


So it's obvious that the no scheme:

var datestr:string = "2013-8-7 9:30:15";
is not in accordance with the format requirements


Yes scheme:

var datestr:string = "2013-8-7 9:30:15";
var date:date = new Date ();
Date.time = Date.parse (Convertdatestr (DATESTR));//yes, to use Convertdatestr method
//trace:wed 7 09:30:15 GMT+0 The
Trace (date.tostring ());

Convertdatestr () Method:

		/**
		 * DATESTR format must be: Yyyy-mm-dd HH:MM:SS, such as: 2013-8-7 9:30:15;
		 * <listing>
		 * <font size= ' 2 ' >
		 *//e.g:
		 * var datestr:string = "2013-8-7 9:30:15";
		 * var date:date = new Date ();
		 * Date.time = Date.parse (Convertdatestr (DATESTR));
		 *//trace:wed 7 09:30:15 gmt+0800
		 * Trace (date.tostring ());
		 * </font>
		 * </listing>
		 * @author jave.lin
	 	 * @date 2013-8-7
		 * * */public
		static function Convertdatestr (datestr:string): string{
			var strarr:array = Datestr.split ("");
			var fstr:string = "{0} {1} {2}";
			Return Format (FSTR, (strarr[0] as String). Split ("-"). Join ("/"), Strarr[1], "GMT");

		/** method In previous format article */public
		static function format (str:string, ... args): string{for
			(var i:int = 0; i< Args.length; i++) {
				str = str.replace (new RegExp ("\\{" + i + "\ \}", "GM"), Args[i]);
			}
			return str;
		}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.