iphone - Fuzzy Date algorithm in Objective-C -


I would like to type a fuzzy date method for the calculation of dates in the Objective-C for the iPhone Here's a popular comment: / P>

Although there are missing arguments in how can this be used in Objective-C? Thank you.

const int SECOND = 1; Const int MINUTE = 60 * seconds; Const int HOUR = 60 * min; Const int DAY = 24 * hours; Const int monthly = 30 * days; If (delta and lieutenne; 1 * minute) {return ts.Seconds == 1? "A second ago": ts.Seconds + "seconds ago"; } If (delta and lieutenne; 2 * min) {return "one minute ago"; } If (Delta & Lt; 45 * Min) {Return T. Minutes + "minutes ago"; } If (delta and lt; 90 * minute) {back "an hour ago"; } If (delta and lieutenant; 24 * hours) {return t. Hers + "hours ago"; } If (delta and lieutenant; 48 hours) {return "tomorrow"; } If (Delta & Lt; 30 * days) {Return TSD + "Day First"; } If (delta and lieutenant; 12 * month) {int months = convert toint32 (Math. Floor ((double) ts day / 30)); Return month & lt; = 1? "A month ago": months + "months ago"; } And {int year = convert.toInt32 (Math. Floor ((double) ts day / 365)); Return Year & lt; = 1? "A year ago": year + "years ago"; }

Code of NSDate . Between two dates is a convenient way to get Delta in seconds in NSDate , it is called on a NSDate example, another as an argument Taking the object NSDate returns a one (which is typed for double), which represents the number of seconds between two dates.

In view of this, it will be very easy to optimize the code given above, because an Objective-C / Coco reference is given in delta seconds calculated by NSDTE Two dates are given, you can easily customize the above code:

  // Fixed #define SECOND 1 #define MINUTE (60 * seconds) # defined hours (60 minutes) # Define defined days (24 * hours) # months (30 days) - (NSString *) Time interval: Explosion start date: (NA Dit *) degree with D1 (Ansditi *) Calculate D 2 {/ / delta in seconds between the two dates NSTimeInterval delta = [d2 Smayinrvlsins date: D1]; If (delta and lieutenne; 1 * minute) {return delta == 1? @ "A second ago": [NSString stringWithFormat: @ "% d seconds ago", (intestine) delta]; } If (delta and lieutenne; 2 * min) {return @ "one minute ago"; } If (Delta & Lt; 45 * min) {int min = floor ((double) Delta / MINUTE); Return [NSString stringWithFormat: @ "% d minutes ago", min]; } If (delta and lt; 90 * min) {back @ "an hour ago"; } If (delta and lieutenant; 24 * hours) {int hours = floor ((double) delta / hor); Return [NSString stringWithFormat: @ "% d hours ago", hours]; } If (delta and lieutenant; 48 * hours) {@ "tomorrow" @ return; } If (delta and lieutenant; 30 * days) {int day = floor ((double) delta / day); Return [NSString stringWithFormat: @ "% d day ago", day]; } If (delta and lieutenant; 12 * month) {int month = floor ((double) delta / mnsh); Return month & lt; = 1? @ "A month ago": [NSString stringWithFormat: @ "% d months ago", months]; } And {int year = floor ((double) delta / mant / 12.0); Return Year & lt; = 1? @ "A year ago": [NSString stringWithFormat: @ "% d years ago", year]; }}  

It will be started, giving start and end of NSDate objects as arguments, and returning to NSString Time interval


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -