本文介紹了從字符串創建 NSDate的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個像這樣的 NSString
:@"3/15/2012 9:15 PM"
我想把它轉換成 NSDate代碼>,我是這樣做的:
I have an NSString
like this: @"3/15/2012 9:15 PM"
and I would like to convert it to NSDate
, I have done like this:
NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];
NSLog(@"%@", date); // date = null
你能幫幫我嗎,謝謝.
推薦答案
使用如下解決方案
NSString *str = @"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM/dd/yyyy HH:mm a";
NSDate *date = [formatter dateFromString:str];
NSLog(@"%@", date);
對不起,格式應該如下:
Sorry, the format should be as follows:
formatter.dateFormat = @"MM/dd/yyyy hh:mm a";
顯示的時間將是格林威治標準時間.因此,如果您添加/減去時區,將是晚上 9:15.
And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.
#2
如下使用.你也會得到準確的時間.
Use as below. You would get exact time too.
NSString *str = @"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM/dd/yyyy hh:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
formatter.timeZone = gmt;
NSDate *date = [formatter dateFromString:str];
NSLog(@"%@",date);
這篇關于從字符串創建 NSDate的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!