你需要花2步字符串:
- 字符串轉換爲
NSDate
- 轉換日期到時間戳
像這樣:
- (void) dateConverter{
NSString *string = @"22/04/2013 05:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm"];
NSDate *date = [[NSDate alloc] init];
// voila!
date = [dateFormatter dateFromString:string];
NSLog(@"dateFromString = %@", date);
//date to timestamp
NSTimeInterval timeInterval = [date timeIntervalSince1970];
}
然後實現像前一次下面的方法將幫助,雖然你卜我相信你可以修改它來幫你這不是完全!
- (NSString *) timeAgoFor : (NSDate *) date {
double ti = [date timeIntervalSinceDate:[NSDate date]];
ti = ti * -1;
if (ti < 86400) {//86400 = seconds in one day
return[NSString stringWithFormat:@"Today"];
} else if (ti < 86400 * 2) {
return[NSString stringWithFormat:@"Yesterday"];
}else if (ti < 86400 * 7) {
int diff = round(ti/60/60/24);
return[NSString stringWithFormat:@"%d days ago", diff];
}else {
int diff = round(ti/(86400 * 7));
return[NSString stringWithFormat:@"%d wks ago", diff];
}
}
'NSDateFormatter' ......所有的 – 2013-07-24 14:27:35
首先請你告訴我,誰擁有downvote這個問題,爲什麼? – iMash
這個論壇是不是降不投票仔細閱讀並理解它的任何問題。如果上面的問題有一些差異,你可以改正它,而不是投票,否則你可以通過解決問題來解決問題。 – iMash