views:

328

answers:

4

I know that - SQLite doesn't support Day name & 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 )

+(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 month name from month no string
( %m month: 01-12 )

+(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 ?

Thanks in advance for sharing your knowledge.

Sagar.

+1  A: 

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

newtover
Will it be available to iPhone app development? as it seems to be osx library.
sugar
+2  A: 

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:

Nate
Hmm. I think you miss something. Sat 6 February -> Sat 6th February. similarly Sat 2nd February.
sugar
As far as I know, you would have to do that yourself. Ordinals (6th instead of 6, 2nd instead of 2) vary greatly between spoken languages and are not directly supported in most programming languages. If possible, *don’t* specify the output format and instead allow the system to use its default output format, which will be determined by the user’s locale.
Nate
+1  A: 

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"];
        }
    }   
}
sugar
A: 

good very good......................

rkloves