如何从这个字符串"Monday, 27 April 2015 12:12:10 am India Standard Time"中获取长格式的时间?



当我在UIDatePicker中选择任何时间(仅时间模式)时,我得到这个字符串"星期一,27 四月 2015 12:12:10 am India Standard Time"。

当我这样做时

 `NSLog(@"%@",[self.timePicker.date descriptionWithLocale:[NSLocale currentLocale]]);`

我可以在控制台中查看我选择的时间以及日期和其他时区详细信息。

 Monday, 27 April 2015 12:12:10 am India Standard Time

如何仅提取时间部分并将时间部分(上午 12:12:10)转换为长格式?

将 NSDate 转换为长格式的代码

 long timeInLong=[date timeIntervalSince1970]*1000;

但是请告诉我如何从该控制台字符串中获取时间部分。

我已经尝试过这个,利亚姆得到"null"作为输出

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:@"HH:mm aa"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:[self.timePicker.date descriptionWithLocale:[NSLocale currentLocale]]];
    NSLog(@"%@",dateFromString); //null

请帮忙

更新:

现在我使用以下代码获得这样的输出

output    2015-04-27 13:17:56 +0000

法典:

   NSDate* sourceDate = self.timePicker.date;
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

    NSString *dateOutput = [[NSString alloc] initWithFormat:@"%@", destinationDate];
    NSLog(@"%@",dateOutput);

我现在仍然能够分离那个时间部分.

这是解决方案

    NSDate* sourceDate = self.timePicker.date;
    NSString *dateString = [NSDateFormatter localizedStringFromDate:sourceDate
                                                          dateStyle:NSDateFormatterNoStyle
                                                          timeStyle:NSDateFormatterMediumStyle];
    NSLog(@"%@",dateString);

输出格式为下午 4:00:00

相关内容

最新更新