简体   繁体   English

字符串中的nsdate仅适用于日期而非时间

[英]nsdate from string works only with date not time

im using a string to convert it to an nsDate 即时通讯使用字符串将其转换为nsDate

is working fine, but when i try to add time to my string I get null on the logs 工作正常,但是当我尝试向字符串添加时间时,日志中为空

NSString *dateStringus = [NSString stringWithFormat:@"12-09-2011"];
NSLog(@"%@",dateStringus);

NSDateFormatter *dateFormattero = [[NSDateFormatter alloc] init];

[dateFormattero setDateFormat:@"dd-MM-yyyy"];

NSDate* d = [dateFormattero dateFromString:dateStringus];
NSLog(@"tomad tu string date ::%@",d);    

NSString *pingus = [dateFormattero stringFromDate:d];

NSLog(@"transumatada ::%@", pingus);

NSLog(@"\n");

Works fine for only date, but 仅在日期有效,但

if I do 如果我做

NSString *dateStringus = [NSString stringWithFormat:@"12-09-2011 14:14:00"];

I get null on the logs 我在日志中得到null

how to fix this string to include time? 如何解决此字符串以包括时间? thanks! 谢谢!

The dateFormat property of the NSDateFormatter dateFormattero must match the format of date/time in the string dateStringus . NSDateFormatter dateFormatterodateFormat属性必须与字符串dateStringus的日期/时间格式匹配。

Change: 更改:

[dateFormattero setDateFormat:@"dd-MM-yyyy"];

Into: 进入:

[dateFormattero setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

Here is a good reference on date format patterns. 这是有关日期格式模式的很好的参考

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

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