简体   繁体   English

如何在iOS中以Nsdate格式获取时间?

[英]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作为时间,我想在NSDate时间内转换此字符串,但是我不知道我得到了什么........

     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 输出date1:​​2000-01-01 11:30:00 +0000
I want only correct time 17:00:00 as NSDate format ...How Can get this? 我只希望NSDate格式的正确时间为17:00:00 ...如何获得此信息? .... ....
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. NSLog –准确:-description(NSDate)–以UTC格式打印日期。

Unexpected value from NSDate NSDate的意外值

BTW: You should set the date style to none. 顺便说一句:您应该将日期样式设置为无。

This is because NSDate is a combination of Date and Time. 这是因为NSDate是日期和时间的组合。 When you print any NSDate, it will print in this format. 当您打印任何NSDate时,它将以这种格式打印。 But an NSString from this NSDate display what you really want. 但是此NSDate中的NSString显示您真正想要的。 So just fetch NSString from your date as 所以只需从您的日期中获取NSString作为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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