2014-01-23 154 views
1

我使用的代碼更改日期格式時收到錯誤:在格式化日期

NSDateFormatter *dateFormat=[NSDateFormatter new]; 
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 


    dateFormat.locale=locale; 

    [dateFormat setDateFormat:@" dMMyyyy HH:mm:ss"]; 
    NSDate *date = [dateFormat dateFromString:Currentdate]; 
    NSDate *today = [dateFormat dateFromString:[datef stringFromDate:[NSDate date]]]; 
    NSLog(@"date %@ %@",date,Currentdate); 

    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSUInteger unitFlags = NSHourCalendarUnit|NSMinuteCalendarUnit 
    ; 
    NSDateComponents *components = [gregorian components:unitFlags 
               fromDate:date 
                toDate:today options:0]; 

但我得到的錯誤:

*** -[__NSCFCalendar components:fromDate:]: date cannot be nil 
I mean really, what do you think that operation is supposed to mean with a nil date? 
An exception has been avoided for now. 
A few of these errors are going to be reported with this complaint, then further. violations will simply silently do whatever random thing results from the nil. 
Here is the backtrace where this occurred this time (some frames may be missing due to  compiler optimizations): 
    (
0 CoreFoundation      0x30ffbad5 <redacted> + 92 
1 CoreFoundation      0x30ffba63 <redacted> + 74 
2 VoxSci        0x000d6997 -[MessagesViewController DayCounting] + 602 
3 VoxSci        0x000d858d -[MessagesViewController tableView:cellForRowAtIndexPath:] + 4000 
4 UIKit        0x33930315 <redacted> + 408 
5 UIKit        0x338d86cd <redacted> + 1800 
6 UIKit        0x338d7ef1 <redacted> + 184 
7 UIKit        0x337fe353 <redacted> + 346 
8 QuartzCore       0x33484943 <redacted> + 142 
9 QuartzCore       0x33480167 <redacted> + 350 
10 QuartzCore       0x3347fff9 <redacted> + 16 
11 QuartzCore       0x3347fa0d <redacted> + 228 
12 QuartzCore       0x3347f81f <redacted> + 314 
13 QuartzCore       0x3347954d <redacted> + 56 
14 CoreFoundation      0x31044f69 <redacted> + 20 
15 CoreFoundation      0x310428f7 <redacted> + 286 
16 CoreFoundation      0x31042c43 <redacted> + 738 
17 CoreFoundation      0x30fad471 CFRunLoopRunSpecific + 524 
18 CoreFoundation      0x30fad253 CFRunLoopRunInMode + 106 
19 GraphicsServices     0x35ce72eb GSEventRunModal + 138 
20 UIKit        0x33862845 UIApplicationMain + 1136 

22 libdyld.dylib      0x3b8cfab7 <redacted> + 2 

我已經搜索一下但得到了類似的東西。可以有人告訴我這個問題或我錯在哪裏。 任何幫助將不勝感激。提前致謝。

回答

0

首先,我假設你的意思是ddMMyyyy HH:mm:ss [DATEFORMAT setDateFormat:@ 「dMMyyyy HH:MM:SS」];

其次,它將取決於您的Currentdate設置爲。它必須符合你的NSDateForatter或它不會工作。例如

NSString *Currentdate = @"2014-01-01 01:00:00"; 
NSString *Currentdate2 = @"01012014 01:00:00"; 

NSDateFormatter *dateFormat=[NSDateFormatter new]; 
[dateFormat setDateFormat:@"ddMMyyyy HH:mm:ss"]; 
NSDate *date = [dateFormat dateFromString:Currentdate]; 
NSLog(@"Currentdate %@",date); 

將記錄遵循

2014-01-22 23:14:27.958 TableViewCellWithAutoLayout[94829:a0b] Currentdate (null) 
2014-01-22 23:14:27.959 TableViewCellWithAutoLayout[94829:a0b] Currentdate2 = 2014-01-01 07:00:00 +0000 

爲什麼第一次約會空,第二個日期是否有效?這是因爲格式在第二個日期是正確的,但是第一個格式不正確(對於這個NSDateFormatter)。

而且這條線

NSDate *today = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]]; 

相同此行

NSDate *today = [NSDate date]; 

你只是從的NSDate轉換爲字符串的NSDate。

的日期從格式@"Thur, 2 Jan 2014 19:40:18 GMT"我要剝去字符串GTMThur,和使用的格式d MMM yyyy HH:mm:ss

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"]; 
NSString *dateString = @"Thur, 2 Jan 2014 19:40:18 GMT"; 
dateString = [dateString stringByReplacingOccurrencesOfString:@"GMT" withString:@""]; 

NSRange range = [dateString rangeOfString:@", " options:NSBackwardsSearch range:NSMakeRange(0, [dateString length]-1)]; 
NSLog(@"range.location: %lu", range.location); 
dateString = [dateString substringFromIndex:range.location+1]; 

NSDate *date = [dateFormatter dateFromString:dateString]; 
NSLog(@"date = %@",date); 
+0

好吧,我明白了,你能告訴方式比較兩個日期格式「星期四,2014年1月2日19時40分18秒」並區分它們嗎? –

+0

@PrachiRajput - 更新我的迴應 – ansible

+0

非常感謝,它真的幫助過我 :) –

0

您的Currentdate變量將是nil

您的日期格式爲下面的代碼錯誤[dateFormat setDateFormat:@" dMMyyyy HH:mm:ss"];

+0

這就是我問,在這裏我錯了轉換? –

+0

什麼是您的「當前日期」。我的意思是相關代碼在哪裏? –

+0

你的日期格式有誤.. –

0

可使用,這將幫助你。

NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0 
                   locale:[NSLocale currentLocale]]; 
date = [fmt dateFromString:[NSString stringWithFormat:@"%@ %@", obj.meetingTime, obj.meetingDate]]; 

查看下面的鏈接瞭解更多描述。

What kind of sarcastic error is this iOS?

+0

感謝您的回覆,我已經使用此代碼,但沒有幫助:( –

+0

可能是您的字符串包含空格,你必須修剪它[日期字符串stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] - –

1

我認爲你有一個錯字:) 嘗試

[DATEFORMAT setDateFormat:@ 「DDMMYYYY HH:MM:SS」];

1

你的代碼應該是

NSString *Currentdate = @"2013-08-06 03:51:54"; 

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

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

NSDate *date = [dformat dateFromString:datestr]; 

請參考蘋果的文檔NSDateFormatter Class Reference

+0

但日期格式爲「Thur,2014年1月2日19:40:18 GMT」,並且我使用了格式「EE,d LLLL y​​yyy HH:mm:ss'GMT'」但沒有任何幫助 –

+0

參考蘋果的文檔我發佈了一個鏈接在答案 –