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
A useful and helpful collection library of common Objective-C functions, methods, classes and categories.
Showing posts with label date. Show all posts
Showing posts with label date. Show all posts
September 26, 2013
September 12, 2013
Check if a string is a valid time format
Ever wonder to check if a NSString might represent a valid time? I mean, hours : minutes : seconds ...
/** Check whether a string is a valid time in standard format "HH:mm:ss".
@param time String time to evaluate.
@return Returns true if it's a valid string time, false otherwise.
*/
+ (BOOL)isValidTimeString:(NSString *)time {
NSArray *temp = [time componentsSeparatedByString:@":"];
if ([temp count] != 3) { return NO; }
NSString *h = [temp objectAtIndex:0];
NSString *m = [temp objectAtIndex:1];
NSString *s = [temp objectAtIndex:2];
// Minutes and seconds must be of two digits. Hours, instead, might have any digits.
if (m.length != 2) { return NO; }
if (s.length != 2) { return NO; }
// Check values are numeric.
NSCharacterSet *num09 = [NSCharacterSet decimalDigitCharacterSet];
if (![num09 isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:h]]) { return NO; }
if (![num09 isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:m]]) { return NO; }
if (![num09 isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:s]]) { return NO; }
// Minute and second values must be lesser than 60.
if ([m integerValue] >= 60) { return NO; }
if ([s integerValue] >= 60) { return NO; }
return YES;
}
Download the ready-for-use source file (.m) of Check if a string is a valid time format
/** Check whether a string is a valid time in standard format "HH:mm:ss".
@param time String time to evaluate.
@return Returns true if it's a valid string time, false otherwise.
*/
+ (BOOL)isValidTimeString:(NSString *)time {
NSArray *temp = [time componentsSeparatedByString:@":"];
if ([temp count] != 3) { return NO; }
NSString *h = [temp objectAtIndex:0];
NSString *m = [temp objectAtIndex:1];
NSString *s = [temp objectAtIndex:2];
// Minutes and seconds must be of two digits. Hours, instead, might have any digits.
if (m.length != 2) { return NO; }
if (s.length != 2) { return NO; }
// Check values are numeric.
NSCharacterSet *num09 = [NSCharacterSet decimalDigitCharacterSet];
if (![num09 isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:h]]) { return NO; }
if (![num09 isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:m]]) { return NO; }
if (![num09 isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:s]]) { return NO; }
// Minute and second values must be lesser than 60.
if ([m integerValue] >= 60) { return NO; }
if ([s integerValue] >= 60) { return NO; }
return YES;
}
Download the ready-for-use source file (.m) of Check if a string is a valid time format
July 10, 2013
Get current year
How to get current year in Objective-C using the Cocoa framework?
It works with both iOS (iPhone, iPad, iPod) and Mac OS systems.
/** Get current year.
@return Returns the current year as integer.
*/
+ (NSInteger)getCurrentYear {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];
return [components year];
}
Download the ready-for-use source file (.m) of Get current year
It works with both iOS (iPhone, iPad, iPod) and Mac OS systems.
/** Get current year.
@return Returns the current year as integer.
*/
+ (NSInteger)getCurrentYear {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];
return [components year];
}
Download the ready-for-use source file (.m) of Get current year
Subscribe to:
Posts (Atom)