Conversion Time Problem

I have time in formate "02/17/2014 05:00 PM", now I want to convert it to 17-02-2014 05:00: 00 + 000. I am using the code. this date = 02/17/2014 and time = 5:00 p.m.

-(NSDate*)dateFromDate:(NSString*)date andTime:(NSString*)time{
    NSString *eventDttm = [[NSString alloc] init];
    eventDttm = [NSString stringWithFormat:@"%@ %@",date,time];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *local = [NSLocale currentLocale];
    [dateFormatter setLocale:local];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm a"];

    //conversion of NSString to NSDate

    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:eventDttm];

    return dateFromString;
}

Using this code, I get 02/17/2014 11:00: 00 + 000

+4
source share
5 answers

you can use this code

NSString *str=@"17-02-2014 05:00 PM";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm a"];
NSDate *datetoday=[dateFormatter dateFromString:str];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
NSString *strdate=[dateFormatter stringFromDate:datetoday];
 NSLog(@"%@",strdate);
+4
source

I think your problem is "HH"in your date format string. "HH"It is designed for 24 hours, but your entry is 12 hours. Try to use "HH".

: http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

, , :

NSString *eventDttm = [[NSString alloc] init];

- NSString eventDttm.

eventDttm = [NSString stringWithFormat:@"%@ %@",date,time];

- NSString eventDttm. , , , ARC ​​ .

, dateFromString.

+2

HH hh. HH , 24- . 12 .

+1

... xcode "prettyDate".

   NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:@"17-02-2014 05:00 PM"];

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];



NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:eventDttm];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
NSString *prettyDate = [dateFormatter stringFromDate:dateFromString];
prettyDate = [prettyDate stringByAppendingString:@" +000"];

, !!!

...!!!

+1

Please use this code,

NSString *dateStr = @"17-02-2014 05:00 PM";  
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];  
[dateFormatter1 setDateFormat:@"dd-MM-yyyy hh:mm a"];

NSDate *date = [dateFormatter1 dateFromString:dateStr];  
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];  
[dateFormatter2 setDateFormat:@"dd-MM-yyyy hh:mm:ssZZ"];

dateStr = [dateFormatter2 stringFromDate:date];
 NSLog(@"date : %@",dateStr);

using this code, you will receive a string, date: 17-02-2014 05: 00: 00 + 0530 Thank you.

0
source

Source: https://habr.com/ru/post/1527492/


All Articles