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 time. Show all posts
Showing posts with label time. Show all posts
September 26, 2013
September 17, 2013
Get the number of seconds in a time string
How many seconds there are in a given time string value?
/** Converts from string time to seconds.
Works only if used with this time format "HH:mm:ss".
@param str Time string in format "HH:mm:ss".
@return Returns the total number of seconds as NSNumber.
@see stringTimeFromSeconds:
*/
+ (NSNumber *)secondsInTimeString:(NSString *)str {
NSNumber *ret = [NSNumber numberWithLong:-1];
NSMutableArray *tempArr = [[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:@":"]];
if ([tempArr count] == 3) {
ret = [NSNumber numberWithLong: [[tempArr objectAtIndex:0] integerValue]*60*60 + [[tempArr objectAtIndex:1] integerValue]*60 + [[tempArr objectAtIndex:2] integerValue] ];
}
return ret;
}
Download the ready-for-use source file (.m) of Get the number of seconds in a time string
/** Converts from string time to seconds.
Works only if used with this time format "HH:mm:ss".
@param str Time string in format "HH:mm:ss".
@return Returns the total number of seconds as NSNumber.
@see stringTimeFromSeconds:
*/
+ (NSNumber *)secondsInTimeString:(NSString *)str {
NSNumber *ret = [NSNumber numberWithLong:-1];
NSMutableArray *tempArr = [[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:@":"]];
if ([tempArr count] == 3) {
ret = [NSNumber numberWithLong: [[tempArr objectAtIndex:0] integerValue]*60*60 + [[tempArr objectAtIndex:1] integerValue]*60 + [[tempArr objectAtIndex:2] integerValue] ];
}
return ret;
}
Download the ready-for-use source file (.m) of Get the number of seconds in a time string
September 16, 2013
Get time format from seconds
I have an amount of seconds that I would convert to a human readable string. What can I do?
Simply use my Objective-C method to accomplish it!
You need:
- http://objective-c-functions.blogspot.com/2013/08/add-zeros-to-digit-number.html
/** Converts from seconds to time string.
Returned time format is "HH:mm:ss".
@param sec Number of seconds as NSNumber.
@return Returns the string time as "HH:mm:ss".
@see secondsInTimeString:
*/
+ (NSString *)stringTimeFromSeconds:(NSNumber *)sec {
NSString *ret;
if ([sec longValue] < 0) {
LogError(@"Seconds are negative: %ld", sec.longValue);
ret = @"Undefined";
} else {
NSInteger h = floorf( [sec longValue] / 3600 );
NSInteger m = floorf( ([sec longValue]/60) % 60 );
NSInteger s = floorf( [sec longValue] % 60 );
ret = [NSString stringWithFormat:@"%@:%@:%@",
[LMFunctions twoDigits:h], [LMFunctions twoDigits:m], [LMFunctions twoDigits:s]];
}
return ret;
}
Download the ready-for-use source file (.m) of Get time format from seconds
Simply use my Objective-C method to accomplish it!
You need:
- http://objective-c-functions.blogspot.com/2013/08/add-zeros-to-digit-number.html
/** Converts from seconds to time string.
Returned time format is "HH:mm:ss".
@param sec Number of seconds as NSNumber.
@return Returns the string time as "HH:mm:ss".
@see secondsInTimeString:
*/
+ (NSString *)stringTimeFromSeconds:(NSNumber *)sec {
NSString *ret;
if ([sec longValue] < 0) {
LogError(@"Seconds are negative: %ld", sec.longValue);
ret = @"Undefined";
} else {
NSInteger h = floorf( [sec longValue] / 3600 );
NSInteger m = floorf( ([sec longValue]/60) % 60 );
NSInteger s = floorf( [sec longValue] % 60 );
ret = [NSString stringWithFormat:@"%@:%@:%@",
[LMFunctions twoDigits:h], [LMFunctions twoDigits:m], [LMFunctions twoDigits:s]];
}
return ret;
}
Download the ready-for-use source file (.m) of Get time format from seconds
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
Subscribe to:
Posts (Atom)