Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to Convert String into Date

Explorer ,
Sep 11, 2010 Sep 11, 2010

hell there everybody,

i have a question which is:

how do we convert string which we get by reading a XML into date type instance value???

so there is a XML which has modifiedDate as an atribute on one node of the XML and how i do put that value into a variable of date instance??

the timeformat in the XML is: mm/dd/yyyy hh:mm:ss => 12/31/2010 23:57:46

i choose this time format because i want to use the .parse() method which still cause some error but this is for another topic..,

hope i make myself clear about the question,

thanks in advance for the help.

TOPICS
ActionScript
3.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 11, 2010 Sep 11, 2010

First you should look at the Date object constructor to see how to specify the different parameters.

public function Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0)

Then you need to determne how to break down the String using the String.split() method.  The first split() should be the space between date and time, the second for the slashes in the date, and the third for the colons in the time.

Then you just t

...
Translate
LEGEND ,
Sep 11, 2010 Sep 11, 2010

First you should look at the Date object constructor to see how to specify the different parameters.

public function Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0)

Then you need to determne how to break down the String using the String.split() method.  The first split() should be the space between date and time, the second for the slashes in the date, and the third for the colons in the time.

Then you just take each of those arrays of substrings and feed them into the contructor as Number() values.

If you remain unsure how to proceed, look up the various terms I've identified in the help documents and you will find them all explained along with examples.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 11, 2010 Sep 11, 2010

hi ned thanks for responding to this thread..,

hahaha..,

i tought that there is some other way around this that simpler and easier yet if that is the only way than that it will..,

thanks ned..,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2010 Sep 11, 2010

I may not understand the question but parse DOES work with the date format you showed - there is no need for any additional manipulations:

var xmlString:String = "12/31/2010 23:57:46";
var date:Date = new Date(Date.parse(xmlString));

trace("parse", Date.parse(xmlString));
trace("month", date.month);
trace("date", date.date);
trace("year", date.fullYear);
trace("hours", date.hours);
trace("minutes", date.minutes);
trace("seconds", date.seconds);

Output:

parse 1293857866000
month 11
date 31
year 2010
hours 23
minutes 57
seconds 46

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2010 Sep 11, 2010

You understand it and knew about a method I didn't (no surprise)... thanks.  Learning new stuff is one reason why I play here.  I tried doing a search on Google and it started me chasing some additional downloadable stuff, which I didn't care to pursue.  Nice to know there's something readily available.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2010 Sep 11, 2010
LATEST

I think Date class is not well documented. As a matter of my experience, Date is one of the toughest datatypes to get grasp on in all the programming language. I guess the reason is that once they decided to count from December 31 1969 (var date:Date = new Date(0)). Why 1969 - not 3000 BC or 1950 -  I don't know. I suspect someone was happy Nixon took office :-).

In other words, Date as a Number, although convenient, is counterintuitive. This is why for the majority of us it is rather a matter of experience - not knowledge. In other words, unless one has a project that heavily deals with dates - it is confusing. Throw in time zones and you are, basically, paralyzed for awhile 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines