0

My date and time is 20-Nov-2019 21:09 Which is in UTC 24 hours format. now I want to convert it into local time in 12 hours formate. 30-Nov-2019 08:00 AM like this.

My code is :

// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];

My code when i send my local time 12 formate into 24 hours UTC

-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
//    NSLog(@"%@", localDate);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm"];
    NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    dateFormatter.locale = twelveHourLocale;
    NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
    NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
    NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
    NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
//    NSLog(@"dateString %@", dateString);
    return dateString;
}

-(NSDate *)getUTCDate:(NSString *)currentDate{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MMM-yyyy HH:mm"];

    NSDate *date1 = [dateFormat dateFromString:currentDate];
    if (date1 == nil){
         [dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm a"];
         date1 = [dateFormat dateFromString:currentDate];
    }
    return date1;
}
Naresh
  • 16,698
  • 6
  • 112
  • 113

1 Answers1

0

I think you are doing too much. The format "hh" is the hour in 12-hour format, HH is 24-hour format. You should not have to set the locale for that (though setting to en_US_POSIX does avoid the user's 24-hour preference in the [NSLocale currentLocale] instance which can override that on iOS).

NSDate is an absolute instance in time. You need to apply a calendar and time zone with an NSDateFormatter to get numeric year/month/day etc. values out of it, but you don't need to adjust the offset to the reference date (that is changing the actual date, not just reformatting it in a different time zone).

NSString *utcString = @"20-Nov-2019 21:09";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
formatter.dateFormat = @"dd-MMM-yyyy HH:mm";

NSDate *date = [formatter dateFromString:utcString];

formatter.timeZone = [NSTimeZone localTimeZone]; // GMT-5 for me
formatter.dateFormat = @"dd-MMM-yyyy hh:mm a";
NSLog(@"date: %@", [formatter stringFromDate:date]);
// date: 20-Nov-2019 04:09 PM
Carl Lindberg
  • 2,902
  • 18
  • 22