如何仅存储没有时区和实际时间的日期

时间:2015-04-17 07:05:07

标签: ios objective-c iphone xcode

我只是在学习xcode,我试图在互联网上找到答案,但没有运气  我的简单问题:如何只存储没有时区的日期和核心数据的实际时间? 目前我使用NSDateFormatterNSDate存储日期:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateT = [dateFormatter dateFromString:self.DateLabel.text];
[ItemDate setValue: dateT forKey:@"date"];

但是如果我使用这部分代码并将存储日期打印为日期:2015-04-17 07:00:00 +0000

基本上我只想将其删除:07:00:00 +0000并仅存储2015-04-17

有可能吗?

3 个答案:

答案 0 :(得分:3)

Alex,获得输出日期后,您可以尝试使用此代码。

NSString *str1 = @"2015-04-17 07:00:00 +0000";
NSRange range = [str1 rangeOfString:@" "];

NSString *date = [str1 substringToIndex:range.location];
NSLog(@"%@",date);

答案 1 :(得分:0)

试试这个

    NSString *yourJSONString = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzzz"];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *output = [dateFormatter stringFromDate:dateFromString];
    NSLog(@"%@", output);

答案 2 :(得分:0)

How to store dates without times in Core Data中的答案是对您问题的回答(以及对NSDate的一些解释)。