- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 NSDateFormatter 的 dateFromString,但是当它被赋予字符串“2013-03-06”时,它吐出一个 NSDate“2012-03-06 00:00:00 CST”。时间并不重要。我将日期格式设置为在输入和输出之间完全相同。
我有一个使用代码获取日期设置的对象:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
然后这个值稍后用于在 UIDatePicker 上设置日期:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
如果能指出我犯错的地方,我们将不胜感激。
最佳答案
您应该使用 yyyy
而不是 YYYY
。
A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar
关于iOS 'dateFromString' 返校一年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15253111/
我正在使用此代码将字符串转换为日期: +(NSDate*)dateTexteFromString:(NSString*)someString { NSDateFormatter *df = [
我有一个将 NSString 转换为 NSDate 的函数。 + (NSDate *)getNSDateFromString:(NSString *)strDate andFormat:(NSStri
我正在使用 NSDateFormatter 的 dateFromString,但是当它给出字符串时,它会输出不同的小时。 代码 NSDateFormatter *dateformatter = [[N
我绝对不是 NSDateFormatter 或 NSDate 的新手,但这完全难倒了我。我到底是怎么弄错开火日期的? NSDateFormatter *firedateFormat = [[NSDat
尝试将字符串传递给 NSDateFormatter dateFromString 方法时发生崩溃。 我查看了文档,但没有看到任何可以解释问题所在的内容。 NSError * error
我正在使用 NSDateFormatter 的 dateFromString,但是当它被赋予字符串“2013-03-06”时,它吐出一个 NSDate“2012-03-06 00:00:00 CST”
2014/03/30 02:00 -> 02:59 我正在解析 CSV 以训练基本神经网络。每行对应一分钟。该代码可以毫无问题地解析 2014 年的剩余时间,但那一小时的结果为 nil 值。符号上没有
我正在尝试将字符串转换为日期,但结果是不正确的日期。 let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd MMM
我有以下代码: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat =
我正在尝试使用 NSDate dateFromString 方法,但收到警告并且它导致应用程序崩溃。代码如下: NSString *pickerDate = [NSString stringWithF
这是我的NSString, myDate : 2011-06-07 11:23:47 0000 这是代码, NSTimeZone *currentDateTimeZone = [NSTimeZone
美好的一天, 我有一个NSDateFormatter,它似乎不喜欢我尝试转换的特定字符串。这个特定的实例是 2009-10-16T09:42:24.999605 我所有其他日期的格式都相同,并且可以解
以下代码: NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.d
请用以下代码帮助我。我想做的就是看到两个日期之间的时间(4:42:55)相差不到24小时,所以我不在乎日期。 谢谢 NSLog(@"error code"); NSDate *now = [[NSDa
我有以下代码: NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setDateFormat:
在 swift 中使用 dateFromString 时,某些值得到 nil。我搜索了很多但徒劳无功。 以下是我的代码: let strDate = self.sortedDict.valueForK
- (NSDate*) parseTime:(NSString*) time { NSDateFormatter* dateFormatter = [[NSDateFormatter allo
我知道这个问题已经在 SO 上讨论过很多次,但没有一个结果能解决我的问题。这是代码片段, NSDateFormatter* df = [[NSDateFormatter alloc]init]; [d
我知道对此有很多问题,但似乎没有一个答案对我有用。这是我的代码(iOS SDK 6.0) NSDateFormatter *df = [NSDateFormatter alloc]; [df setD
我知道问题已经存在,但给出的答案对我没有帮助。 生日的值为:Jun 29, 2013, 8:45 AM 这是我的代码: NSDate* now = [[NSDate alloc] init]; NSD
我是一名优秀的程序员,十分优秀!