Reputation: 1
How can I compare a string that comes in this format:
var date1:String = "16:30";
to server clock in ActionScript 3?
Upvotes: 0
Views: 853
Reputation: 8149
Pretty simple. You just need to move the timestamp into a usable format using the split()
method and then compare it to the local time using the Date
class.
var time:String = "16:30";
var a:Array = time.split(":"); // == ["16", "30"]
var hours:Number = Number(a[0]);
var minutes:Number = Number(a[1]);
var curTime:Date = new Date();
var curHour:Number = curTime.hours; // 0-23 format. Adjust if needed
var curMinute:Number = curTime.minutes; //0-59 format. Adjust if needed
if (curHour > hours) {
// current time is greater than the timestamp
}
else if (curHour == hours && curMinute > minutes) {
// current time is greater than the timestamp
}
else if (curHour == hours && curMinute == minutes) {
// current time is equal to the timestamp
}
else {
// current time is less than the timestamp
}
You could obviously consolidate those conditions, but I did it this way to properly show the logic.
I'm unsure if there is a built in way to do this (as there is in other languages, like PHP), but I doubt it given the way AS3 handles time (there are no formatting options, no ways to add or subtract time, etc. Everything is expected to be handled by the programmer instead).
Upvotes: 1