Friday, October 12, 2007

Birthday project - Date issues

As the results of a conversation I had I am developing a special program for my birthday. While there are other programs that do what mine does, I didn't write them and they don't allow for the modifications that I desire so I wrote my own version. I have decided that it might be a neat idea to spend the few days before the official release releasing my development diary for this "game". I am not going to reveal what the project is, so readers can have fun trying to guess what exactly it is that I developed.

The first issue with the development of Birthday is dates. In particular, this project needs to know the difference between two dates. The problem is that the ActionScript date class doesn't have built in comparison functions for getting this information. It does, however, have a function for getting the number of milliseconds that have passed since January 1, 1970. By using this number, you can very easily calculate differences in the date. The problem is what happens if you need a date that is before 1970? While for personally using this project this is not an issue, some people who are using this could easily have been born before 1970 (both of my parents were). Looking at the date class, it lets you enter any year that you want, so what would happen if you entered a date before 1970 and then grabbed the milliseconds?

The answer was actually exactly what you would expect. The millisecond value returned was a negative value! This actually works for me. By having a negative value, you would still be able to calculate the number of days between two dates, which is exactly what I need to do for this project. This is a simple calculation as if we subtract the smaller date from the larger one and then divide the result by 86,400,000 then we will know how many days there are between the two dates.

No comments: