Howdy fellow geeks. I have what I think is a simple problem and I hope you can
help.
I have a date in a variable.
var myDate = "8/5/2005"
I need code to extract the month, day and year.
var mth = ??
var dy = ??
var yr = ??
Thanks for your help.
Troy
Rothrock - 31 Aug 2005 18:11 GMT
You could myDate.split("/");
But is there a reason you don't want to use the date class?
TroyK - 31 Aug 2005 18:13 GMT
I'll use anything. How could I use the date class to do this?
var myDate = "8/5/2005"
var my_date:Date = new Date(myDate);
this doesn't work because new Date() wants it split up already
NSurveyor - 31 Aug 2005 18:17 GMT
You don't need the date class... just use split like Rothrock suggested:
var myDate = "8/5/2005";
var split_date = myDate.split("/");
var mth = split_date.split("/")[0];
var dy = split_date.split("/")[1];
var yr = split_date.split("/")[2];
Split splits a string into an array on a specific character. So, splitting on
'/' will give you the array: ["8","5","2005"].
NSurveyor - 31 Aug 2005 18:18 GMT
Sorry... what was I thinking... should be:
var myDate = "8/5/2005";
var split_date = myDate.split("/");
var mth = split_date[0];
var dy = split_date[1];
var yr = split_date[2];
TroyK - 31 Aug 2005 18:20 GMT
to put it in the date class so I could use it's features would be nice too. So
It looks like this works.
var myDate = "8/5/2005";
var t = myDate.split("/")
var my_date:Date = new Date(t[2],t[0]-1,t[1]);
Great! Thanks guys
NSurveyor - 31 Aug 2005 18:26 GMT
Not neccessarily. myDate is a string. t is an array of strings. (with number
values). You should use either Number(t[x]) or parseInt(t[x],10) - in case you
have a leading 0 for the month and date. So, good code would be:
var myDate = "8/5/2005";
var t = myDate.split("/");
var my_date:Date = new Date(parseInt(t[2],10),parseInt(t[0],10)
-1,parseInt(t[1],10) );
TroyK - 31 Aug 2005 18:28 GMT
Crap, here's another problem. What if I had:
var myDate = "8/5/2005 8:30 AM";
How could I assign that to a date class variable?
NSurveyor - 31 Aug 2005 18:36 GMT
Just keep using split!
var myDate = "8/5/2005 8:30 AM";
//
var parts = myDate.split(" ");
date_string = parts[0];
time_string = parts[1];
am_pm = parts[2];
//
var t = date_string.split("/");
var u = time_string.split(":");
//
var year = parseInt(t[2],10);
var month = parseInt(t[0],10)-1;
var date = parseInt(t[1],10);
//
var hour =(am_pm=="PM") ? parseInt(u[0],10)+11 : parseInt(u[0],10)-1;
var minute = parseInt(u[1],10);
//
var my_date = new Date(year,month,date,hour,minute);
Rothrock - 31 Aug 2005 18:44 GMT
First question I would have is where are these text dates coming from?If they
are just things that you are assigning, then why not start off with a date
object?
var myDate= new Date(2005,4,8,8,30);
Or what have you.
TroyK - 31 Aug 2005 18:48 GMT
very cool, thx.
I'm not to sure why this line
var hour =(am_pm=="PM") ? parseInt(u[0],10)+11 : parseInt(u[0],10)-1;
doesn't read
var hour = (am_pm == "PM") ? parseInt(u[0], 10)+12 : parseInt(u[0], 10);
But other than that I'm good. thanks again!
TroyK - 31 Aug 2005 18:50 GMT
Rothrock, I'm getting them from an SQL database which stores it as 8/5/2005 8:30 AM.
I just didn't want to have that involved of a question :)
Thanks
Troy
NSurveyor - 31 Aug 2005 18:53 GMT
Sorry about that... didn't know what I was thinking...
You're welcome..
NSurveyor - 31 Aug 2005 19:08 GMT
Actually I think there is one error. On a 12 hour clock, you have 12:00 AM to
12:00 PM to 12:00 AM. Flash counts hours from 0 to 23 where 0 is midnght and 23
is noon. So, 12 midnight should be 0. In the code above,12:00 AM is treated as
12 oclock AM in the next day...
try:
var hour = (am_pm == "PM") ? parseInt(u[0], 10)+12 : parseInt(u[0], 10);
if(hour==24) hour = 0;
Think of it this way... in flash 12 midnite comes before 1 am, not after 11
pm. We were treating the other way.
NSurveyor - 31 Aug 2005 19:09 GMT
To prove my point:
var myDate = "8/5/2005 12:00 PM";
//
var parts = myDate.split(" ");
date_string = parts[0];
time_string = parts[1];
am_pm = parts[2];
//
var t = date_string.split("/");
var u = time_string.split(":");
//
var year = parseInt(t[2],10);
var month = parseInt(t[0],10)-1;
var date = parseInt(t[1],10);
//
var hour =(am_pm=="PM") ? parseInt(u[0],10)+12 : parseInt(u[0],10);
var minute = parseInt(u[1],10);
//
var my_date = new Date(year,month,date,hour,minute);
trace(myDate);
trace(my_date);
Rothrock - 31 Aug 2005 22:40 GMT
Ah that makes sense. Since I just saw you hard coding it I just had to ask. You know ? sometimes we can't see the forest for the trees. :)