[英]How to manipulate a NSDate from a NSString
我試圖從字典中獲取推特帖子的日期並操縱數據以顯示“Seconds Ago”,“1天前”,等等。 我已經下載了一個名為FormatterKit的AFNetworking制造商的API表格。 我的問題是我應該如何從下面的字符串中刪除:
NSString *postDate = t[@"created_at"];
到可以傳遞給API的可操作日期? (我不知道如何將它傳遞給API。我在自述文件中找到的只是下面的代碼。)
TTTTimeIntervalFormatter *timeIntervalFormatter = [[TTTTimeIntervalFormatter alloc] init];
[timeIntervalFormatter stringForTimeInterval:0]; // "just now"
[timeIntervalFormatter stringForTimeInterval:-100]; // "1 minute ago"
[timeIntervalFormatter stringForTimeInterval:-8000]; // "2 hours ago"
// Turn idiomatic deictic expressions on / off
[timeIntervalFormatter stringForTimeInterval:-100000]; // "1 day ago"
[timeIntervalFormatter setUsesIdiomaticDeicticExpressions:NO];
[timeIntervalFormatter stringForTimeInterval:-100000]; // "yesterday"
// Customize the present tense deictic expression for
[timeIntervalFormatter setPresentDeicticExpression:@"seconds ago"];
[timeIntervalFormatter stringForTimeInterval:0]; // "seconds ago"
// Expand the time interval for present tense
[timeIntervalFormatter stringForTimeInterval:-3]; // "3 seconds ago"
[timeIntervalFormatter setPresentTimeIntervalMargin:10];
[timeIntervalFormatter stringForTimeInterval:-3]; // "seconds ago"
字典鍵值:
"created_at" = "Sun Mar 23 21:18:10 +0000 2014";
您似乎需要計算與TTTTimeIntervalFormatter
一起使用的時間間隔。
而且似乎你開始使用某些(我們未知)格式的日期字符串。
第一步是將您的日期字符串轉換為NSDate
。 使用NSDateFormatter
設置為與您的字符串匹配的格式來執行此操作。 如果您還不知道,有很多關於如何執行此步驟的現有主題。
將“發布日期”作為NSDate
,您可以計算出這樣的時間間隔:
NSDate *postDate = ... // the date calculated from your date string
NSTimeInterval interval = [postDate timeIntervalSinceNow];
現在過去你的TTTTimeIntervalFormatter
interval
。
您需要為這些方法提供時間增量 (發布時間與現在之間的差異):
NSDate *postTime = ...;
NSTimeInterval delta = [postTime timeIntervalSinceNow];
NSString *deltaDescr = [timeIntervalFormatter stringForTimeInterval:timeInterval];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.