是否可以定义一个适应可变数据的日期格式?

发布于 2024-12-25 21:19:35 字数 509 浏览 3 评论 0原文

我需要找到一种可以适应以下两个日期的日期格式:

  1. 2012-01-22T14:27:31.176+0000
  2. 2012-01-22T14:27:31+0000 code>

如您所见,唯一的区别是日期 2 没有秒小数部分。

两种日期格式为:

  1. yyyy-MM-dd'T'HH:mm:ss.SZ
  2. yyyy-MM-dd'T'HH:mm:ssZ

以及第二个省略 .S

是否可以使 .S 可选?或者我必须继续使用两种不同的格式?

问题在于,我从 API 调用中获取日期,有时它带有小数秒,有时则没有。

我想我可以检测小数位的存在并相应地使用两种不同的日期格式,但我一直在寻找更干净的解决方案。

谢谢!

I need to find a date format which could adapt to the two following dates:

  1. 2012-01-22T14:27:31.176+0000
  2. 2012-01-22T14:27:31+0000

As you can see, the only difference is that date 2 does not have the fractional seconds.

The two date formats would be:

  1. yyyy-MM-dd'T'HH:mm:ss.SZ
  2. yyyy-MM-dd'T'HH:mm:ssZ

with the second one omitting .S.

Is it possible to make .S optional? Or do I have to keep using 2 different formats?

The problem lies in the fact that I'm getting the date from an API call, and sometimes it comes with the fractional seconds, other times without.

I guess I could detect the presence of the decimal place and use the two different date formats accordingly, but I was looking for a cleaner solution.

Thanks!

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

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

发布评论

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

评论(5

不羁少年 2025-01-01 21:19:35

您可以将NSDateFormatterlenient属性设置为YES,这允许NSDateFormatter对您的输入进行猜测细绳。但是,您应该对此进行测试,因为它可能会给您带来一些误报。

You can set the NSDateFormatter's lenient property to YES which allows the NSDateFormatter to take a guess on your input string. However, you should test this since it might give you some false positives.

梨涡 2025-01-01 21:19:35

嗯,第一件事可能是查看数据的实际长度。不干净,但应该可以工作

Well, the first thing could be to look at the actual length of the data. Not clean, but should work

残疾 2025-01-01 21:19:35

您可以测试字符串的长度,然后根据长度使用两种不同的日期格式化程序之一吗?

Could you test the length of the string and then use one of 2 different date formatters depending on the length?

謸气贵蔟 2025-01-01 21:19:35

我将有两个日期格式化程序,然后根据日期字符串中是否有小数位来选择要使用的日期格式化程序。

我不认为有一个格式字符串可以使小数成为可选的

或者您可以只使用带有小数秒的格式字符串,没有小数秒的日期只会在末尾显示 .00,可能看起来更一致?

I would have two date formatters, and then choose the date formatter to use depending on wether there is a decimal place or not in your date string.

I dont think there is a format string that would make the decimal optional

Alternatively you could just use the format string with the fractional seconds, your dates without fractional seconds would just display with a .00 on the end, might look more consistent?

最笨的告白 2025-01-01 21:19:35

在这种情况下,@JustSid 建议的“宽松”属性不起作用。

由于没有找到“更干净”的解决方案,我回到了第一个解决方案,即检测小数位的存在并相应地使用两种不同的日期格式。

NSRange textRange =[targetDate rangeOfString:@"."];

if(textRange.location == NSNotFound) {
    format = @"yyyy-MM-dd'T'HH:mm:ssZ";
} else {
    format = @"yyyy-MM-dd'T'HH:mm:ss.SZ";
}

The "lenient" property suggested by @JustSid in this case did not work.

Having not found a "cleaner" solution, I went back to my first one, which was detecting the presence of the decimal place and using the two different date formats accordingly.

NSRange textRange =[targetDate rangeOfString:@"."];

if(textRange.location == NSNotFound) {
    format = @"yyyy-MM-dd'T'HH:mm:ssZ";
} else {
    format = @"yyyy-MM-dd'T'HH:mm:ss.SZ";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文