格式转换日期yyyy-MM-dd'T'HH:mm:ss.SSS [英] Converting date of format yyyy-MM-dd'T'HH:mm:ss.SSS
问题描述
我有几个 NSDate
包含符合此格式的值的对象 yyy-MM-dd'T'HH:mm:ss.SSS
当我尝试转换为不同格式,例如 MMM dd,yyyy HH:mm
formatter总是返回 nil
。
但是,如果我硬编码字符串值, c $ c> NSDate 对象,它被转换为新的格式没有问题。
我在模型中使用 setValue:forKey填充我的
NSDate
code>方法,我觉得问题是这里的JSON库传递一个字符串值。但是,调试显示 NSDate
已包含通过JSON返回的值。
任何想法导致这种奇怪行为?
这是我的转换代码:
NSDateFormatter * format = [[NSDateFormatter alloc] init];
[format setDateFormat:@yyyy-MM-dd'T'HH:mm:ss.SSS];
NSString * foundDateStr = [format stringFromDate:modelObj.createdDate];
[format setDateFormat:@MMM dd,yyyy HH:mm];
NSDate * formattedDate = [format dateFromString:createdDateStr];
NSLog(@========= REal Date%@,[format stringFromDate:formattedDate]);
编辑:重述您的问题,除了任何其他的概念误解:
我有几个NSDate对象包含符合以下格式的值
据我所知(我不是一个Objective-C程序员,不可否认)一个 NSDate
object不知道任何关于格式化。它只是代表一个时间的瞬间。它不知道是从文本解析还是以其他方式创建的。
它像一个 int
- int
变量不是十进制或十六进制,它只是一个数字。它是当你格式化和解析你指定。同样,您可以使用 NSDateFormatter
来描述您在解析和格式化时需要的格式
您目前只有一个 NSDateFormatter
到yyyy-MM-dd'T'HH:mm:ss.SSS
,然后重新设置 c> dateFromString 。您提供的代码段格式 modelObj.createdDate
为ISO-8601格式,但随后尝试使用MMM dd,yyyy解析 HH:mm格式。
我建议你有两个单独的 NSDateFormatter
对象:
NSDateFormatter * isoFormat = [[NSDateFormatter alloc] init];
[isoFormat setDateFormat:@yyyy-MM-dd'T'HH:mm:ss.SSS];
NSString * createdDateStr = [format stringFromDate:modelObj.createdDate];
NSDateFormatter * shortFormat = [[NSDateFormatter alloc] init];
[shortFormat setDateFormat:@MMM dd,yyyy HH:mm];
NSDate * parsedDate = [isoFormat dateFromString:createdDateStr];
NSLog(@========= REal Date%@,[shortFormat stringFromDate:parsedDate]);请注意,我已经更改了您的 formattedDate
$ b <变量到 parsedDate
,因为它是真正的 - 一个 NSDate
它是通过解析文本创建的。 >
如注释中所述,除非你事实上需要这样格式化和解析,你不应该。我包含了所有原始代码,只是为了显示您如何格式化日期,然后使用相同的 NSDateFormatter
重新解析。你将得到相同的结果:
NSDateFormatter * shortFormat = [[NSDateFormatter alloc] init];
[shortFormat setDateFormat:@MMM dd,yyyy HH:mm];
NSLog(@========= REal Date%@,[shortFormat stringFromDate:modelObj.createdDate]);
一般来说,您应避免不必要的转换。将从文本转换为更自然的数据类型(在这种情况下 NSDate
,但同样适用于数字等),并使用该数据类型尽可能多的代码,只会在最后一刻转换为文字。
I have few NSDate
objects which contain values compliant to this format yyy-MM-dd'T'HH:mm:ss.SSS
When I try to convert to a different format such as MMM dd, yyyy HH:mm
the formatter always returns nil
.
However, if I hardcode the string value I get through the NSDate
object , it is transformed to the new format without a problem.
I populate my NSDate
object in the model using the setValue:forKey:
method, and I feel the problem is here where the JSON library passes a string value. However, the debugging shows that the NSDate
has contains the value returned via JSON.
Any idea what's causing this strange behavior?
This is my conversion code:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *createdDateStr = [format stringFromDate:modelObj.createdDate];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];
NSDate *formattedDate = [format dateFromString:createdDateStr];
NSLog(@"========= REal Date %@",[format stringFromDate:formattedDate]);
EDIT: Rereading your question, I think you've got a conceptual misunderstanding aside from anything else:
I have few NSDate objects which contains values compliant to the following format
As far as I'm aware (I'm not an Objective-C programmer, admittedly) an NSDate
object doesn't know anything about formatting. It just represents an instant in time. It isn't aware of whether it was parsed from text or created in some other way.
It's like an int
- an int
variable isn't in decimal or hex, it's just a number. It's when you format and parse that you specify that. Likewise, you use NSDateFormatter
to describe the format you want at the point of parsing and formatting.
You've currently only got one NSDateFormatter
- you're setting the format to "yyyy-MM-dd'T'HH:mm:ss.SSS"
then resetting it before you've called dateFromString
. The code snippet you've given formats modelObj.createdDate
into ISO-8601 format, but then tries to parse it using the "MMM dd, yyyy HH:mm" format.
I suggest you have two separate NSDateFormatter
objects:
NSDateFormatter *isoFormat = [[NSDateFormatter alloc] init];
[isoFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *createdDateStr = [format stringFromDate:modelObj.createdDate];
NSDateFormatter *shortFormat = [[NSDateFormatter alloc] init];
[shortFormat setDateFormat:@"MMM dd, yyyy HH:mm"];
NSDate *parsedDate = [isoFormat dateFromString:createdDateStr];
NSLog(@"========= REal Date %@",[shortFormat stringFromDate:parsedDate]);
Note that I've changed your formattedDate
variable to parsedDate
, as that's what it really is - an NSDate
which has been created by parsing text.
As noted in comments, unless you actually need to format and parse like this, you shouldn't. I included all of your original code just to show how you can format a date and then reparse it with the same NSDateFormatter
. You'll get the same result with just:
NSDateFormatter *shortFormat = [[NSDateFormatter alloc] init];
[shortFormat setDateFormat:@"MMM dd, yyyy HH:mm"];
NSLog(@"========= REal Date %@",[shortFormat stringFromDate:modelObj.createdDate]);
In general, you should avoid unnecessary conversions. Convert from text to the more natural data type (NSDate
in this case, but the same applies for numbers etc) and use that data type for as much of your code as possible, only converting to text at the last moment.
这篇关于格式转换日期yyyy-MM-dd'T'HH:mm:ss.SSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!