How can i get the time from a datetime string according to my time zone. My time zone time is +5:30 GMT.The datetime looks like-
04/03/2013 3:30:00 AM
I want the output like-
9:00:00 AM
Thanks in advance.
How can i get the time from a datetime string according to my time zone. My time zone time is +5:30 GMT.The datetime looks like-
04/03/2013 3:30:00 AM
I want the output like-
9:00:00 AM
Thanks in advance.
You need two date formatters.
One to convert string to date. And then date to string to get required in time format.
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *date=[dateFormatter dateFromString:@"04/03/2013 3:30:00 AM"];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setDateFormat:@"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
*Not compiled and check
If you have date and time in string which is date time in GMT then you need to add GMT in your string. By this you can get actual time of your time zone -
NSString *dateStr = @"04/03/2013 3:30:00 AM GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a ZZZ"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:@"hh:mm:ss a"];
NSString *opDateStr = [dateFormatter stringFromDate:date];
NSLog(@"op = %@",opDateStr);
NSString *strdate= @"04/03/2013 3:30:00 AM";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date=[dateFormatter dateFromString:strdate];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setDateFormat:@"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
NSLog(@"%@",time);
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"%@",[formatter stringFromDate:[NSDate date]]);
Output 06/03/2013 10:41:18 AM
[formatter setDateFormat:@"hh:mm:ss a"];
NSLog(@"%@",[formatter stringFromDate:[NSDate date]]);
Output 10:41:18 AM
instead of [NSDate date] pass the required date to be formatted.
As you want the time from the time string you provided according to the current time zone, you need to do something like this
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a zzz"];
NSDate *date=[dateFormatter dateFromString:@"04/03/2013 3:30:00 AM 0000"];
NSDateFormatter *dfTime = [NSDateFormatter new];
[dfTime setTimeZone:[NSTimeZone systemTimeZone]];
[dfTime setDateFormat:@"hh:mm:ss a"];
NSString *time=[dfTime stringFromDate:date];
NSLog(@"%@",time);
you wont get the real output unless there is the offset present which is the 0000.(its the GMT offset). Also the time formatter wont work perfectly if the format of the dateString and its formatter are different. So you need to do it MM/dd/yyyy hh:mm:ss a zzz, zzz is the offset. Now your output will be 09:00:00 AM