View Full Version : Converting selectedDate to YYYYMMDD-format
MrFlashALot
January 13th, 2005, 05:48 AM
Im trying to use the DateChooser. You can get the date by using "selectedDate"-function, but the format is so screwed up that I cant use it. Looks like this:
Wed Jan 12 00:00:00 GMT+0100 2005
Is there a quick way to turn this string into a string with "YYYYMMDD"-format (ie. "20050123")?
Ive looked trough the help-files to see if the DateChooser-class has any quick-conversion functions, but I cant find any..
kode
January 13th, 2005, 06:17 AM
I don't remember if there's a built-in function for it, but it's not that hard to create your own method:
function convertTimestamp(timestamp) {
var t = timestamp.split(" ");
var y = t[5];
for (var i = 1; i<=12; i++) {
if (t[1] == ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][i-1]) {
var m = i<10 ? "0"+i : i;
}
}
// replace the next line with "var d = t[2];" if the timestamp already includes leading zeros
var d = parseFloat(t[2])<10 ? "0"+t[2] : t[2];
return y+m+d;
}
var time = "Wed Jan 12 00:00:00 GMT+0100 2005";
trace(convertTimestamp(time)); // output: 20050112
;)
MrFlashALot
January 13th, 2005, 06:51 AM
Wow, that was quick! :)
Yep, the code works great when the "time"-variable loads a string of the date.
But apparently I was wrong when I assumed it was a string that one got from DateChooser, because when I store it in the "time"-variable, I get "NaN" in trace.
//////////////////////////
// LISTENER //
//////////////////////////
var selDate = new Object();
selDate.change = function(eventObj){
// get: Recieve date and turn into string - DOESNT WORK
var s_XMLvalue:String = toString(eventObj.target.selectedDate);
function convertTimestamp(timestamp) {
var t = timestamp.split(" ");
var y = t[5];
for (var i = 0; i<12; i++) {
if (t[1] == ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][i]) {
var m = i+1<10 ? "0"+(i+1) : i+1;
}
}
var d = parseFloat(t[2])<10 ? "0"+t[2] : t[2];
return y+m+d;
}
var time = s_XMLvalue; // instead of "Wed Jan 17 00:00:00 GMT+0100 2005"
trace(convertTimestamp(time)); // output: NaN
}
myDateChooser.addEventListener("change", selDate);
Above is the listener that is triggered when you click on a date on the DateChooser.
I even made a lame attempt at converting the date to a string (using "toString()" ), but didnt work either.
Any ideas ? :)
kode
January 13th, 2005, 07:09 AM
I get it now... it's because the selectedDate property returns a Date object, which I didn't know. See? That's what I get for never using components. :P
This should do it:
var t = eventObj.target.selectedDate;
var y = t.getFullYear().toString();
var m = (t.getMonth()+1<10 ? "0"+(t.getMonth()+1) : t.getMonth()+1).toString();
var d = (t.getDate()<10 ? "0"+t.getDate() : t.getDate()).toString();
var s_XMLvalue = y+m+d;
trace(s_XMLvalue);
MrFlashALot
January 13th, 2005, 07:21 AM
Dude, youve got some serious skills, which I truly appreciate - thanks again :D
Yeah, components...thought Id save a lot of time by starting to use them, but so far its been more work than its worth, it seems ;)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.