NSString 的 NSDate 给出 null 结果

发布于 2024-12-19 12:22:37 字数 961 浏览 5 评论 0原文

我正在使用以下代码生成 NSDate -> NSString

+(NSString *)getCurrentTime
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSString* str =[dateFormatter stringFromDate:now];
    [dateFormatter release];
    NSLog(@"%@",str);
    return str;
}

上面的代码一切都很好。我正在使用上面的代码将字符串存储在数据库中。现在,在检索该字符串时,我得到了 NULL。以下是我以特定格式检索日期的代码

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:MM:SS a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
    NSLog(@"Date : %@",dt);
    [dateFormatter release];

我应该如何以特定格式检索或存储?我的 crdInfo.swipeTime 正在正确检索字符串...

I am using following code to generate NSDate -> NSString

+(NSString *)getCurrentTime
{
    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSString* str =[dateFormatter stringFromDate:now];
    [dateFormatter release];
    NSLog(@"%@",str);
    return str;
}

everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:MM:SS a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
    NSLog(@"Date : %@",dt);
    [dateFormatter release];

How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心的位置 2024-12-26 12:22:37

首先,为什么不只存储 NSDate 对象或纪元时间戳呢?这将为您将来提供更大的灵活性。

现在您的问题,我怀疑这是由于您对 NSDateFormatter 的配置造成的,您将其保存为一种格式并尝试使用不同的格式将其转换为日期。使格式相同并重试。如果您想以不同于存储的方式显示它,您可能需要使用存储的格式将其转换为 NSDate,然后再次使用另一个日期格式化程序将其转换为您想要的字符串格式。

First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.

Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.

伴我老 2024-12-26 12:22:37

正如 Narayana 建议的那样,您需要以与存储的格式相同的格式检索日期。按如下方式检索: -

    NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
    [reDateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
    [reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *dt = [reDateFormatter dateFromString:str];
    NSLog(@"The Date : %@",dt);

    [reDateFormatter setDateFormat:@"hh:MM:SS a"];
    NSString *currentTime = [reDateFormatter stringFromDate:dt];
    NSLog(@"%@",currentTime);

希望对您有帮助。

As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -

    NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
    [reDateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"];
    [reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *dt = [reDateFormatter dateFromString:str];
    NSLog(@"The Date : %@",dt);

    [reDateFormatter setDateFormat:@"hh:MM:SS a"];
    NSString *currentTime = [reDateFormatter stringFromDate:dt];
    NSLog(@"%@",currentTime);

Hope it helps you.

疑心病 2024-12-26 12:22:37

尝试将其格式化为dd-MM-yyyy hh:mm:ss

您编写了 dd-MM-yyyy hh:MM:SS a,其中 hh:MM:SS 中的 MM 给出了此格式无法识别的月份写大写的 SS 几秒钟是没有意义的

希望你能理解。

Try to format it to dd-MM-yyyy hh:mm:ss a.

You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds

Hope you understand it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文