iOS 如何將日期字串轉成NSDate

2021-06-06 05:07:24 字數 798 閱讀 1724

nsstring* datestr = @"wed may 2 22:27:08 +0800 2012";

nsdateformatter* formater = [[nsdateformatter alloc] init];

[formater setdateformat:@"eee mmm d hh:mm:ss zzzz yyyy"];

nsdate* date = [formater datefromstring:datestr];

nslog(@"%@", date);

//nsdate* now = [nsdate now];

double inter = fabs([date timeintervalsincenow]);

if( inter < 60)

nslog(@"1 mins ago!");

else if(inter < 60*60)

nslog(@"1 hours ago!");

else if(inter < 60*60*24)

nslog(@"1 days ago!");

nslog(@"interval is %f min", inter/60);

2. 上面**在真機上執行後,發現取得date 為null,模擬器正常顯示,上網搜尋後發現需要設定local, 果然設定後,真機正常

nslocale* local =[[[nslocale alloc] initwithlocaleidentifier:@"en_us"]  autorelease];

[formatter setlocale: local];

hive 如何將陣列轉成字串 HIve字串函式

1.字元 ascii 碼函式 ascii 語法 ascii string str 返回值 int 說明 返回字串 str 中第乙個字元的ascii 碼 舉例 hive select ascii ba from test ok2.base64 字串 語法 base64 binary bin 返回值 s...

如何將字串轉化為日期

一 問題闡述 本來以為把乙個string轉化成date沒什麼難得,用個 dateformat不就好了嗎?結果卻總報錯,仔細檢查才發現原來這裡面還是有個小細節要注意的。二 正確的 實現 dateformat format new dateformat yyyy mm dd string str 201...

Python如何將字串轉換為日期

問題 你的應用程式接受字串格式的輸入,但是你想將它們轉換為 datetime 物件以便在上面執行非字串操作。解決方案 使用python的標準模組 datetime 可以很容易的解決這個問題。比如 from datetime import datetime text 20ojdxu12 09 20 y...