How to get the current date and time printed out in standard formats?
Using NSDateFormatter is quite a joke, so this is a powerful function.
/** Get the current date and time in any of the standard formats.
Parameter "format" must be one of these values:
0: "dd/MM/yyyy - HH:mm"
1: "dd_MM_yyyy_HH_mm_ss"
2: "yyyy-MM-dd"
3: "HH:mm:ss"
4: "dd/MM/yyyy - HH:mm:ss"
@param format Type of format and therefore also the result type.
@return Returns a string representing the current date and time using selected format.
*/
+ (NSString *)currentDateAndTimeUsingFormatMode:(NSInteger)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
switch (format) {
case 0:
[formatter setDateFormat:@"dd/MM/yyyy - HH:mm"];
break;
case 1:
[formatter setDateFormat:@"dd_MM_yyyy_HH_mm_ss"];
break;
case 2:
[formatter setDateFormat:@"yyyy-MM-dd"];
break;
case 3:
[formatter setDateFormat:@"HH:mm:ss"];
break;
case 4:
[formatter setDateFormat:@"dd/MM/yyyy - HH:mm:ss"];
break;
default:
[formatter setDateFormat:@"dd/MM/yyyy - HH:mm"];
break;
}
return [formatter stringFromDate:[NSDate date]];
}
Download the ready-for-use source file (.m) of Get current date and time in a well-kwnown format
No comments:
Post a Comment