nsdateformatter

NSDate from NSString - dateFromString returns nil

安稳与你 提交于 2019-12-20 07:28:15
问题 I want to get a NSDate object from a string of date, but it returns nil. What am I doing wrong? Thanks! NSString *dateString = @"2012-04-05T13:27:32.369Z"; NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss.SSSZ"]; NSDate* date = [df dateFromString:dateString]; NSLog(@"1%@",date); [df release]; 回答1: The "Z" must be quoted: NSDateFormatter* df = [[NSDateFormatter alloc]init]; NSString *dateString = @"2012-04-05T13:27:32.369Z"; [df setDateFormat:@

NSDateFormatter getting Null

邮差的信 提交于 2019-12-20 05:39:07
问题 I in my project I am getting a string I am passing the string to the dateFormatter but I am getting nil, in the string instead of IST I may get PDT I may get any other formats, NSString* currentDateString = @"2012-11-09 12:31:53.0 **IST**"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S z"]; NSDate *dateFromString = [dateFormatter dateFromString:currentDateString]; 回答1: If you are testing on iOS 6.0+ device/simulator only,

How does one use NSDateFormatter's isLenient option?

时光毁灭记忆、已成空白 提交于 2019-12-20 04:26:08
问题 I can't seem to find any info on this flag, on StackOverflow or elsewhere on the web. Apple's own documentation only says: If a formatter is set to be lenient, when parsing a string it uses heuristics to guess at the date which is intended. As with any guessing, it may get the result date wrong (that is, a date other than that which was intended). Maybe I'm misunderstanding how this is supposed to work, but I can't get it to work at all. My guess was something like this (with a relatively

NSDateFormatter reports June 2, 2013 as being in week zero

拥有回忆 提交于 2019-12-20 04:20:55
问题 I am using NSDateFormatter to convert a series of dates to a week number within a month. The date formatting code I am using is yyyyMMW and everything I have read tells me that W will be between 1-5. But, the 2nd of June 2013 fell on a Sunday (the default start day of the week in the gregorian calendar) and it's week number is reported as 0 even though the start date of the week is calculated correctly: 2013-06-03 14:15:45.611 date=20130531, week=2013055, start of week=20130526 2013-06-03 14

How to format the date to IST?

帅比萌擦擦* 提交于 2019-12-20 02:42:26
问题 I have to format the date as below: 19-JUL-2014 10:27:16 IST How can I do that? Should I send "IST" as string object? I tried - NSDate* sourceDate = [NSDate date]; NSLog(@"Date is : %@", sourceDate); NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; NSLog(@"TimeZone is : %@", currentTimeZone); NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init] ; dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss Z"]; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@",

NSDate from NSString gives null result

核能气质少年 提交于 2019-12-19 12:53:25
问题 I am using following code to generate NSDate -> NSString +(NSString *)getCurrentTime { NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString* str =[dateFormatter stringFromDate:now]; [dateFormatter release]; NSLog(@"%@",str); return str; } everything is fine in above code. I am using above code to store string in

NSDate from NSString gives null result

自作多情 提交于 2019-12-19 12:53:09
问题 I am using following code to generate NSDate -> NSString +(NSString *)getCurrentTime { NSDate *now = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy hh:MM:SS a"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSString* str =[dateFormatter stringFromDate:now]; [dateFormatter release]; NSLog(@"%@",str); return str; } everything is fine in above code. I am using above code to store string in

I get nil when using NSDateFormatter in swift

本小妞迷上赌 提交于 2019-12-19 11:29:48
问题 This is my code : println(myStringDate) // 2015-02-26T17:46:34.000Z let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "YYYY-MM-DDTHH:mm:ss.sssZ" let myDate = dateFormatter.dateFromString(myStringDate) println(myDate) // nil Why nil ? What am I doing wrong ? 回答1: Your problem is how you set the date format. Because there is a T in your string, you need to "escape" it and say, that it shouldn't affect the formatting of the date. dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss

Swift DateFormatter Optional Milliseconds [duplicate]

旧街凉风 提交于 2019-12-19 10:28:03
问题 This question already has answers here : How to convert a date string with optional fractional seconds using Codable in Swift4 (3 answers) Closed 10 months ago . I have the following code to parse an ISO8601 date. let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" Problem is sometimes the date is in a format like 2018-01-21T20:11:20.057Z , and other times it's in a format like 2018-01-21T20:11:20Z . So basically part of the time it has the .SSS

NSDateFormatter issue with date format in iOS 8

半世苍凉 提交于 2019-12-19 09:42:16
问题 I have an NSDateFormatter which I use to format an NSDate into a string. The following format does not work: ddMMyyyy_hhmmss_SSS. When I try to format an NSDate I get the following output: 18092014_08:49:03,638, which has a colon and a comma I didn't specify in the format. Note that in iOS 7 I never had this kind of problem. This is the code I'm using: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat: @"ddMMyyyy_hhmmss_SSS"]; NSString *dateString =