Created
January 13, 2015 06:26
-
-
Save zeayes/491f2ced1e379fd49de9 to your computer and use it in GitHub Desktop.
objective c datetime formatter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main(int argc, const char *argv[]) { | |
@autoreleasepool { | |
// formatter datetime | |
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_us"]; | |
[formatter setLocale:locale]; | |
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | |
NSDate *date = [formatter dateFromString:@"2014-01-10 13:42:14"]; | |
NSLog(@"date from string is: %@", date); | |
NSDate *today = [NSDate date]; | |
NSString *dateString = [formatter stringFromDate:today]; | |
NSLog(@"today string is: %@", dateString); | |
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-(24 * 60 * 60)]; | |
NSString *yesterdayString = [formatter stringFromDate:yesterday]; | |
NSLog(@"yesterday string is: %@", yesterdayString); | |
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:+(24 * 60 * 60)]; | |
NSString *tomorrowString = [formatter stringFromDate:tomorrow]; | |
NSLog(@"tomorrow string is: %@", tomorrowString); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment