简体   繁体   English

检索当前日期,不带前导零

[英]Retrieve current date without leading zeroes

I am trying to compare the date chosen on a calendar (Kal calendar implementation) with the current date. 我正在尝试将日历(Kal日历实现)上选择的日期与当前日期进行比较。 The issue is that the current date is in MM/dd/yyyy format, whereas the Kal dates will have no leading zeroes if the month or day is below 10. Is there an easy way to retrieve the current date so that it will not have leading zeroes for the day or month if it's under 10? 问题在于当前日期采用MM / dd / yyyy格式,而如果月份或日期小于10,则Kal日期将没有前导零。是否有一种简单的方法来检索当前日期,使其不具有如果日期或月份小于10,则该日期或月份中的前导零? (The current date will actually be utilized as an attribute for saved objects, so that they can later be queried using the date selected with Kal) (当前日期实际上将用作保存对象的属性,以便以后可以使用用Kal选择的日期查询它们)

example current date - 07/07/2014 当前日期示例-2014/07/07

example Kal date - 7/7/2014 示例Kal日期-2014年7月7日

Don't compare strings holding dates. 不要比较保存日期的字符串。 Create actual NSDate objects, using an NSDateFormatter , and compare those. 使用NSDateFormatter创建实际的NSDate对象,并进行比较。 The format strings you need are "MM/dd/yyyy" for your date, however you're retrieving it, and "M/d/yyyy" for Kal's date. 您需要的格式字符串是“ MM / dd / yyyy”作为日期,但是您正在检索它,而“ M / d / yyyy”则作为Kal的日期。

I have used en_US_POSIX along with "MM/dd/yyyy" & it's Working Fine 我已经将en_US_POSIX“ MM / dd / yyyy”一起使用,并且工作正常

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MM/dd/yyyy"

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM