gpt4 book ai didi

ios - NSDateFormatter 的 NSDate 输出不同

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:22:53 24 4
gpt4 key购买 nike

我在使用 NSDate 时遇到了一些问题,实际上我只需要它来验证它是一个有效日期,因为如果它是一个无效日期,date 实例会返回零。但是

NSString *textValue = @"13/12/2014";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];

// this should cause the output to have no time
[formatter setTimeStyle:NSDateFormatterNoStyle];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSDate *date = [formatter dateFromString:textValue];

date 的结果是 2015-01-12 00:00:00 +0000

我期望的结果应该是:nil

最佳答案

NSString *textValue = @"1/12/2014"; 
NSString *textValue1 = @"15/12/2014";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [formatter dateFromString:textValue];
NSLog(@" >> %@ ",date);
NSDate *date1 = [formatter dateFromString:textValue1];
NSLog(@" >> %@ ",date);

关于ios - NSDateFormatter 的 NSDate 输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24134322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com