DateTime Conversions in Objective C

Here are some commonly used  date time conversions that I wanted in my programming life with Objective C.

Convert the NSString object to a NSDate object

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"RequiredDateFormat"];
NSDate *dateTime = [dateFormatter dateFromString:@“YourDate”];

Convert the NSDate object to a NSString object

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"RequiredDateFormat"];
NSString *currentDateTime = [dateFormatter stringFromDate:[NSDate date]];

Change the timeZone and dateFormat of a NSString dateTime

NSString *timeZonedDateTime = @"TimeZonedDateTime"; 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"CurrentDateTimeFormat"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //Current TimeZone
NSDate *date = [formatter dateFromString:timeZonedDateTime];

[formatter setDateFormat:@"RequiredDateFormat"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"BST"]]; //Required TimeZone
NSString *timeZoneChangesString = [formatter stringFromDate:date];

(If you just want to change the dateTimeFormat but not the timeZone, just ignore the setTimeZone part)

Get the TimeZoneAbbriviation using the timeZone

- (NSString *)getAbbriviationByTimeZone {
 if ([self isEqualToString:@"America/Halifax"])
return @"ADT";
if ([self isEqualToString:@"America/Juneau"])
return @"AKDT";
if ([self isEqualToString:@"America/Argentina/Buenos_Aires"])
return @"ART";
if ([self isEqualToString:@"America/Halifax"])
return @"AST";
if ([self isEqualToString:@"Asia/Dhaka"])
return @"BDT";
if ([self isEqualToString:@"America/Sao_Paulo"])
return @"BRST";
if ([self isEqualToString:@"America/Sao_Paulo"])
return @"BRT";
if ([self isEqualToString:@"Europe/London"])
return @"BST";
if ([self isEqualToString:@"Africa/Harare"])
return @"CAT";
if ([self isEqualToString:@"America/Chicago"])
return @"CDT";
if ([self isEqualToString:@"Europe/Paris"])
return @"CEST";
if ([self isEqualToString:@"Europe/Paris"])
return @"CET";
if ([self isEqualToString:@"America/Santiago"])
return @"CLST";
if ([self isEqualToString:@"America/Santiago"])
return @"CLT";
if ([self isEqualToString:@"America/Bogota"])
return @"COT";
if ([self isEqualToString:@"America/Chicago"])
return @"CST";
if ([self isEqualToString:@"Africa/Addis_Ababa"])
return @"EAT";
if ([self isEqualToString:@"America/New_York"])
return @"EDT";
if ([self isEqualToString:@"Europe/Istanbul"])
return @"EEST";
if ([self isEqualToString:@"Europe/Istanbul"])
return @"EET";
if ([self isEqualToString:@"America/New_York"])
return @"EST";
if ([self isEqualToString:@"Asia/Dubai"])
return @"GST";
if ([self isEqualToString:@"Asia/Hong_Kong"])
return @"HKT";
if ([self isEqualToString:@"Pacific/Honolulu"])
return @"HST";
if ([self isEqualToString:@"Asia/Bangkok"])
return @"ICT";
if ([self isEqualToString:@"Asia/Tehran"])
return @"IRST";
if ([self isEqualToString:@"Asia/Calcutta"])
return @"IST";
if ([self isEqualToString:@"Asia/Tokyo"])
return @"JST";
if ([self isEqualToString:@"Asia/Seoul"])
return @"KST";
if ([self isEqualToString:@"Asia/Hong_Kong"])
return @"HKT";
if ([self isEqualToString:@"Europe/Moscow"])
return @"MSD";
if ([self isEqualToString:@"Europe/Moscow"])
return @"MSK";
if ([self isEqualToString:@"America/Denver"])
return @"MST";
if ([self isEqualToString:@"Pacific/Auckland"])
return @"NZDT";
if ([self isEqualToString:@"Pacific/Auckland"])
return @"NZST";
if ([self isEqualToString:@"America/Los_Angeles"])
return @"PDT";
if ([self isEqualToString:@"America/Lima"])
return @"PET";
if ([self isEqualToString:@"Asia/Manila"])
return @"PHT";
if ([self isEqualToString:@"Asia/Karachi"])
return @"PKT";
if ([self isEqualToString:@"America/Los_Angeles"])
return @"PST";
if ([self isEqualToString:@"Asia/Singapore"])
return @"SGT";
if ([self isEqualToString:@"Africa/Lagos"])
return @"WAT";
if ([self isEqualToString:@"Europe/Lisbon"])
return @"WEST";
if ([self isEqualToString:@"Europe/Lisbon"])
return @"WET";
if ([self isEqualToString:@"Asia/Jakarta"])
return @"WIT";
return nil;
}

(Here I have used it as a NSStringCategory for my use. Instead you can parse NSString date for SELF, according to your requirement)

Hope these will help someone.Cheers!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s