如何将 NSString 转换为 NSDate?

发布于 2024-12-27 08:26:15 字数 528 浏览 3 评论 0原文

我想在我的字典数组中搜索特定的日期。Ex 我想在我的字典数组中搜索日期 "16 Jan 2012" 它是字符串格式,但数组中的字典项包含日期和时间,说“16 Jan 2012 somehours:somemins:somesecs”。我正在将字符串格式日期转换为NSDate格式,但我得到的日期为2012-01-15 18:30:00 +0000 而不是 2012-01-16。我使用 NSPredicate 来搜索将日期转换为秒的日期,如下 "Date == CAST (348345000.000000, "NSDate")" 并进行比较,即使我的记录包含日期为 "16 Jan 2012 somehours:somemins:somesecs" 它不会满足条件。我希望包含日期为 "16 Jan 2012 somehours:somemins:somesecs" 的记录/数组应该满足搜索条件。请问有人知道如何实现这一目标吗?

提前致谢!

I want to search my Array of Dictionary for particular date.Ex I want to search my array of dictionary for date "16 Jan 2012" which is in string format but my dictionary item in array contains date and time, say "16 Jan 2012 somehours:somemins:somesecs".I am converting string format date in NSDate format but I am getting date as 2012-01-15 18:30:00 +0000 instead of 2012-01-16.I am using NSPredicate to search for the date which convert date into seconds as follows "Date == CAST(348345000.000000, "NSDate")" and compare so even though my records contain date as "16 Jan 2012 somehours:somemins:somesecs" it will not satisfied the criteria.I want that the records/array containing date as "16 Jan 2012 somehours:somemins:somesecs" should satisfied the search criteria.Please can anyone know how to achieve this?

Thanks in advance!

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

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

发布评论

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

评论(2

北城半夏 2025-01-03 08:26:15

要摆脱 NSDateFormatter 对时区的自动调整,请使用类似这样的内容。

dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

详细说明; "16 Jan 2012" 不包含小时、分钟或秒值,因此当使用 NSDateFormatter 解析它时,我们会期望 NSDate 的描述为“2012 -01-16 00:00:00 +000" 但这不是你得到的,因为日期格式化程序正在调整之间的 5:30 小时GMT 和印度?(假设基于:30 差异)。通过明确设置日期格式化程序的时区,可以避免此问题。

To rid yourself of NSDateFormatter's automatic adjustment for your time zone use something like this.

dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

To elaborate; "16 Jan 2012" Contains no hour, minute, or seconds value, so when parsing it with an NSDateFormatter one would expect an NSDate with a description of "2012-01-16 00:00:00 +000" But that's not what you're getting because the date formatter is adjusting for the 5:30 Hours between GMT and India?(Assuming based on :30 differential). By setting the date formatter's time zone explicitly you avoid this problem.

甜心小果奶 2025-01-03 08:26:15

您需要使用 NSDateFormatters 将日期字符串从一种格式转换为另一种格式。您可以有一个 inputDateFormatter,您可以在其中使用 dateFromString 为您提供一个 NSDate,然后将其输入到一个 outputDateFormatter 中,它会提供您想要的字符串。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSString *dateInFormatYouWant = [dateFormatter stringFromDate:yourDate]

我不确定是否有比这更有效的方法。

You need to use NSDateFormatters to convert the date string from one format to another. You can have an inputDateFormatter where you use dateFromString to give you an NSDate, and then feed this into an outputDateFormatter which gives your desired string.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSString *dateInFormatYouWant = [dateFormatter stringFromDate:yourDate]

I'm not sure if there is a more efficient way of doing it than this.

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