3

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.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
shivam
  • 1,148
  • 1
  • 12
  • 28
  • check out the `NSDateFormatter` class and its many useful methods. – Cashew Mar 06 '13 at 04:38
  • Chk this http://stackoverflow.com/questions/11504843/get-current-iphone-device-timezone-date-and-time-from-utc-5-timezone-date-and-ti – Aman Aggarwal Mar 06 '13 at 04:38
  • 1
    There are many similar quesions in SO itself. You need to learn the "Date Format Patterns" specified in http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns – Anupdas Mar 06 '13 at 04:42

4 Answers4

6

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

saadnib
  • 11,145
  • 2
  • 33
  • 54
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Thanks. But its output is 3:30:00 AM. How can i get time with my time zone? – shivam Mar 06 '13 at 04:49
  • @shivam: NSDate shows with relative to GMT. You cant set it to IST. – Anoop Vaidya Mar 06 '13 at 04:51
  • I am passing server datetime in NSDate *date=[dateFormatter dateFromString:dateTime];.... Now i want to change it to local time zone. – shivam Mar 06 '13 at 04:54
  • 1
    As you are at then end going to print only time as string. then you can add 5.00 to the `time` which will give you time in IST. You need in IST or some other. If you app saves in local timezone settngs then get the differnce and add it to `time`. – Anoop Vaidya Mar 06 '13 at 04:56
5

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);
saadnib
  • 11,145
  • 2
  • 33
  • 54
2
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);
Gaurav Patel
  • 957
  • 1
  • 6
  • 16
2
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

Meera
  • 1,031
  • 6
  • 25