Frage

I need a NSString with the format "yyyy-mm-dd - 11:00:00"

Year, month, and date should be from the current date, while hour, minute, and second should be 11,00,00. I could not figure how to do that.

War es hilfreich?

Lösung

I think you mean yyyy-MM-dd - HH:mm:ss (because mm is minutes and MM is month). But you just need

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd - HH:mm:ss";
NSString *string = [formatter stringFromDate:[NSDate date]];

Or if you want it to say 11:00:00, either pass it that NSDate, or you could:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd' - 11:00:00'";
NSString *string = [formatter stringFromDate:[NSDate date]];

Andere Tipps

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd";
NSString *string = [NSString stringWithFormat:@"%@ - 11:00:00", [formatter stringFromDate:[NSDate date]]];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top