开发者

How to format a SQLite date in iPhone?

I know that SQLite doesn't support day name and month name. I have gone through this question. I have created two custom functions for it.

My custom function for Converting Day name from day number:

( %w day of week 0-6 with sunday==0 )

Code:

+(NSString*)dayNameStringFromDayNo:(NSString*)dayNo{
    return 
    ([dayNo isEqualToString:@"0"])?@"Sunday":
    ( ([dayNo isEqualToString:@"1"])?@"Monday":
     ( ([dayNo isEqualToString:@"2"])?@"Tuesday":
      ( ([dayNo isEqualToString:@"3"])?@"Wednesday":
       ( ([dayNo isEqualToString:@"4"])?@"Thursday":
        ( ([dayNo isEqualToString:@"5"])?@"Friday": @"Saturday"
        )
       )
      )
     )
    );
}

My custom method for getting mon开发者_如何学编程th name from month no string:

( %m month: 01-12 )

Code:

+(NSString*)monthNameFromNumber:(NSString*)no{
    return ([no isEqualToString:@"01"])?@"January":
    ( ([no isEqualToString:@"02"])?@"February":
     ( ([no isEqualToString:@"03"])?@"March":
      ( ([no isEqualToString:@"04"])?@"April":
       ( ([no isEqualToString:@"05"])?@"May":
        ( ([no isEqualToString:@"06"])?@"June":
         ( ([no isEqualToString:@"07"])?@"July":
          ( ([no isEqualToString:@"08"])?@"August":
           ( ([no isEqualToString:@"09"])?@"September":
            ( ([no isEqualToString:@"10"])?@"October":
             ( ([no isEqualToString:@"11"])?@"November":@"Decemeber"
             )
            )
           )              
          )
         )
        )
       )
      )
     )
    );
}

But what about date following format

Sat 6th February

How to get this kind of output ? Do I have to again create a custom function for it ?


What date format are you getting from SQLite? SQLite does not have a native date/time datatype, but it’s common to store a date as an ISO8601 string of the form YYYY-MM-DD HH:MM:SS or a Unix-style timestamp.

Step 1: Get the date as an NSDate. Here’s one way, using the ISO8601 format mentioned above:

// Create a date formatter
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// Use the formatter to convert the string you retrieved (from
// SQLite) into an NSDate.
NSDate *theDate = [inputFormatter dateFromString:yourSQLiteString];

// Don't forget to release alloc'ed resources
[inputFormatter release];

If your date was stored as a Unix-style timestamp, you could skip using an input formatter and use NSDate dateWithTimeIntervalSince1970: instead.

Step 2: Convert the date to a string:

// Create a date formatter for whatever format you want to output,
// using the formatting patterns from Unicode tr35-6 Appendix F.
outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEE d MMMM"];
NSString *dateString = [dateFormatter stringFromDate:theDate];

// Don't forget to release alloc'ed resources.
[outputFormatter release];

In this example you’ll get a string like Sat 6 February, assuming the user is in an English locale.

References:

  • Data Formatting Programming Guide for Cocoa: Date Formatters
  • Unicode Standard UTS #35: Appendix F: Date Format Patterns


Why not to use formattion capabilities of the client code, or ojective-c does not include strftime?


OK. I have finally added some custom methods for it.

+(NSString*)dayNameStringFromDayNo:(NSString*)dayNo{
    return 
    ([dayNo isEqualToString:@"0"])?@"Sunday":
    ( ([dayNo isEqualToString:@"1"])?@"Monday":
     ( ([dayNo isEqualToString:@"2"])?@"Tuesday":
      ( ([dayNo isEqualToString:@"3"])?@"Wednesday":
       ( ([dayNo isEqualToString:@"4"])?@"Thursday":
        ( ([dayNo isEqualToString:@"5"])?@"Friday": @"Saturday"
         )
        )
       )
      )
     );
}

+(NSString*)dayNameHalfStringFromDayNo:(NSString*)dayNo{
    return 
    ([dayNo isEqualToString:@"0"])?@"Sun":
    ( ([dayNo isEqualToString:@"1"])?@"Mon":
     ( ([dayNo isEqualToString:@"2"])?@"Tues":
      ( ([dayNo isEqualToString:@"3"])?@"Wed":
       ( ([dayNo isEqualToString:@"4"])?@"Thu":
        ( ([dayNo isEqualToString:@"5"])?@"Fri": @"Sat"
         )
        )
       )
      )
     );
}

+(NSString*)monthNameFromNumber:(NSString*)no{
    return ([no isEqualToString:@"01"])?@"January":
    ( ([no isEqualToString:@"02"])?@"February":
     ( ([no isEqualToString:@"03"])?@"March":
      ( ([no isEqualToString:@"04"])?@"April":
       ( ([no isEqualToString:@"05"])?@"May":
        ( ([no isEqualToString:@"06"])?@"June":
         ( ([no isEqualToString:@"07"])?@"July":
          ( ([no isEqualToString:@"08"])?@"August":
           ( ([no isEqualToString:@"09"])?@"September":
            ( ([no isEqualToString:@"10"])?@"October":
             ( ([no isEqualToString:@"11"])?@"November":@"Decemeber"
              )
             )
            )              
           )
          )
         )
        )
       )
      )
     );
}


+(NSString*)monthNameHalfFromNumber:(NSString*)no{
    return ([no isEqualToString:@"01"])?@"Jan":
    ( ([no isEqualToString:@"02"])?@"Feb":
     ( ([no isEqualToString:@"03"])?@"Mar":
      ( ([no isEqualToString:@"04"])?@"Apr":
       ( ([no isEqualToString:@"05"])?@"May":
        ( ([no isEqualToString:@"06"])?@"Jun":
         ( ([no isEqualToString:@"07"])?@"Jul":
          ( ([no isEqualToString:@"08"])?@"Aug":
           ( ([no isEqualToString:@"09"])?@"Sep":
            ( ([no isEqualToString:@"10"])?@"Oct":
             ( ([no isEqualToString:@"11"])?@"Nov":@"Dec"
              )
             )
            )              
           )
          )
         )
        )
       )
      )
     );
}

+(NSString*)suffixNameForNumber:(NSString*)no{
    NSInteger noV=[no intValue];
    if(noV>=11 && noV<=20){
        return [no stringByAppendingString:@"th"];
    } else {
        NSString *l=[no substringFromIndex:1];
        if([l isEqualToString:@"1"]){
            return [no stringByAppendingString:@"st"];
        } else if([l isEqualToString:@"2"]){
            return [no stringByAppendingString:@"nd"];
        } else if([l isEqualToString:@"3"]){
            return [no stringByAppendingString:@"rd"];
        } else {
            return [no stringByAppendingString:@"th"];
        }
    }   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜