简体   繁体   中英

how to get time as Nsdate format in iOS?

hello I want to ask one thing I have NSString as time and i want to convert this string in NSDate time I have do this but I don't know what I got........

     NSString *Time=@"17:00:00";

     NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
    [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter3 setDateFormat:@"HH:mm:ss"];
    NSDate *date1 = [dateFormatter3 dateFromString:Time];
    NSLog(@"date1 : %@", date1);

OUTPUT : date1 : 2000-01-01 11:30:00 +0000
I want only correct time 17:00:00 as NSDate format ...How Can get this? ....
Please solve this Problem....!!!

Set time zone you get proper output try this code,

NSString *Time=@"17:00:00";

NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:Time];
NSLog(@"date1 : %@", date1);

Try this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] 
                 initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *etaStr = @"17:00:00";
    NSDate *generatedDate = [formatter dateFromString:etaStr];
NSLog(@"%@", [formatter stringFromDate:generatedDate]);

This works for me:


    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm:ss";
    NSLog(@"%@", [formatter stringFromDate:[NSDate date]]); // 07:23:38

    NSDate *date = [formatter dateFromString:@"17:00:00"]; // 2000-01-01 15:00:00 +0000
    NSLog(@"%@", date);

Try this,

NSString *Time=@"17:00:00";
NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:Time];
NSLog(@"date1 : %@", date1);

Or use these functions,

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)

 +(NSDateComponents *)componentsForDate:(NSDate *)date 
{
    if (date!=nil)
    {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *dayComponents =
    [gregorian components:DATE_COMPONENTS fromDate:date];

    return dayComponents;
    }

    return nil;
}

+(NSString *)timeFromNSDate:(NSDate *)date
{
    int hour=[self componentsForDate:date].hour;
    int minute=[self componentsForDate:date].minute;
    int second=[self componentsForDate:date].second;

    NSString *returnDateString= [NSString stringWithFormat:@"%d:%d:%d",hour, minute,second];
    return returnDateString;
}

What you get in your log is probably the correct date. NSLog – exactly: -description (NSDate) – prints out the date in UTC.

Unexpected value from NSDate

BTW: You should set the date style to none.

This is because NSDate is a combination of Date and Time. When you print any NSDate, it will print in this format. But an NSString from this NSDate display what you really want. So just fetch NSString from your date as

NSString *dateString = [dateFormatter3 stringFromDate:date1];
 NSLog(@"date : %@ , date string : %@", date1, [dateFormatter3 stringFromDate:date1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM