-8

I am getting time in this format :

29-Mar-2018 05:58:33

which i believe is RFC 850 format.

I need to convert this time to UTC format

2018-03-29T05:58:33Z
iPhoneDeveloper
  • 768
  • 1
  • 10
  • 22
  • I want to convert 29-Mar-2018 time format to UTC format like i have shown above. If you know pls share the answer. I do not know how to convert. – iPhoneDeveloper Mar 29 '18 at 07:10
  • @iPhoneDeveloper do not ask people to "not downvote" your question. If someone downvotes its to highlight an issue with the question. In this case you have been downvoted because converting dates is one of the most common questions here on stackoverflow and there are (literally) hundreds of tutorials online, if you put in the effort to find them. Stackoverflow is run by volunteers who are not paid and don't wish to contribute the same answers to the same questions every day. They ask that you make some effort to solve your own problem and not simply rely on us to do everything for you – Simon McLoughlin Mar 29 '18 at 07:44

3 Answers3

1

Try This.

NSString *strIncomingDate = @"29-Mar-2018 05:58:33";

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:strIncomingDate];

NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormat1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString *strDesire = [dateFormat1 stringFromDate:date];
Kuldeep
  • 3,841
  • 6
  • 25
  • 50
  • Hi Kuldeep. Thanks for the answer. The conversion works fine for year, month and date. But its giving me wrong time format. Since its a 24 hour format i know we are supposed to use HH:mm:ss format but time output is different. – iPhoneDeveloper Mar 29 '18 at 08:45
  • If 29-Mar-2018 07:23:52 is the input i am getting 2018-03-29T12:53:52Z as the output. You can see 07:23:52 is converted to 12:53:52. ? – iPhoneDeveloper Mar 29 '18 at 08:46
  • Getting Time and Converted time is perfect that you define in your question? – Kuldeep Mar 29 '18 at 08:47
  • Hi i was just giving an example of time format. I have updated my code. Pls check and can you tell me how to get proper time format. Since its a 24 hour format we need to use HH:mm:ss instead of hh:mm:ss a right? – iPhoneDeveloper Mar 29 '18 at 08:48
  • result is perfect, if `29-Mar-2018 07:23:52` is `UTC` than as per out timezone, there's `5:30` Hour difference. so we got the output time as `2018-03-29T12:53:52Z` – Kuldeep Mar 29 '18 at 08:49
  • As per your need, if getting time is `29-Mar-2018 07:23:52` than result will be `2018-03-29T07:23:52Z`. am I right? – Kuldeep Mar 29 '18 at 08:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167803/discussion-between-iphonedeveloper-and-kuldeep). – iPhoneDeveloper Mar 29 '18 at 09:45
  • 1
    @iPhoneDeveloper, I update my answer, replace this with old 1. – Kuldeep Mar 29 '18 at 09:58
  • This seems to be the answer required for the question. Thanks Kuldeep bhai :) – iPhoneDeveloper Mar 29 '18 at 11:54
  • @iPhoneDeveloper, You welcome. – Kuldeep Mar 29 '18 at 12:39
0

In Objective C -

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    //The Z at the end of your string represents Zulu which is UTC
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-dd-MM'T'HH:mm:ss'Z'"];
    NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:@"created_at"]];

    //Add the following line to display the time in the local time zone
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"];
    NSString* finalTime = [dateFormatter stringFromDate:newTime];
    [dateFormatter release];
    NSLog(@"%@", finalTime);  

In swift -

    extension Date {
    func GetCurrentTimeZoneDate() -> String {
        let abc = DateFormatter()
        abc.timeZone = TimeZone.current
        abc.dateFormat = "yyyy-MM-dd HH:mm:ss"

        return abc.string(from: self)
    }
}

Call this like:

Date().GetCurrentTimeZoneDate()
Md Rashed Pervez
  • 1,986
  • 8
  • 22
0

I am not putting exact date format you need

I am giving you idea that how you can solve this You have to write it by your self

1) Create date formatter exact with your current format 
NSDateFormatter *timeFormatter1 = [[NSDateFormatter alloc] init];
[timeFormatter1 setDateFormat:@"yyyy-MM-dd hh:mm a"];
2) set time zone of source time zone
[timeFormatter1 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
3) Create date object
NSDate *aDate = [timeFormatter1 dateFromString:result.createdDate];

4) Set Target time zone 
[timeFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
5) Set target date format 
[timeFormatter1 setDateFormat:@"dd-MM-yyyy hh:mm a"];
6) Get string from date
NSString *strTime = [timeFormatter1 stringFromDate:aDate];
Prashant Tukadiya
  • 13,804
  • 3
  • 55
  • 78