简体   繁体   中英

reverse date formatting in as3

As3 has some functions that help with converting a date to various formats of strings, but I cannot find any functions that do the opposite.

in the case of

  • 2012-04-16 I'd like to use YYYY-MM-DD as my format string to get a date object
  • 01/01/2013 12:00.13 I would specify `DD/MM/YYYY HH:NN.SS

Is ther something that does the reverse/opposite of formatDate ?

Leveraging off a similar question ( Parse string pattern into Date in Flex ), you can use DateField.stringToDate to get the date portion of the string:

var theDate:Date
theDate = DateField.stringToDate('2012-04-16', 'YYYY-MM-DD');  // Returns 16 April 2012
theDate = DateField.stringToDate('01/02/2013', 'DD/MM/YYYY');  // Returns 1 Feb 2013

Unfortunately this only parses the date, not the time.

There are examples of using RegExp to parse out the different date-time components and then creating the Date with the values thus parsed, but I've not seen or done anything like the generic format string approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM