+ (NSString*) dateFromString:(NSString*)aStr
{
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date2 = [formater dateFromString:aStr];
[formater setDateFormat:@"d MMM,yyyy HH:mm"];
NSString *result = [formater stringFromDate:date2];
return result;
}
+ (NSString *)calculateTime:(NSString *)datetime :(NSString *)servertime
{
NSString *time;
NSDate *date1;
NSDate *date2;
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
date1 = [formatter dateFromString:datetime];
date2 = [formatter dateFromString:servertime];
}
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit)
fromDate:date1
toDate:date2
options:0];
NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
int hour = interval/3600;
int minute = (int)interval % 3600/60;
int seconds = (int)interval % 60;
hour=ABS(hour);
minute=ABS(minute);
seconds=ABS(seconds);
if ([differenceComponents day]>0) {
time= [NSString stringWithFormat:@"%ld %@", (long)[differenceComponents day],[NSString stringWithFormat:NSLocalizedString(@"daysago", nil)]];
}
else
{
if ([differenceComponents day] == 0) {
time= [NSString stringWithFormat:@"%ld %@", (long)[differenceComponents day],[NSString stringWithFormat:NSLocalizedString(@"dayago", nil)]];
if (hour>0) {
time= [NSString stringWithFormat:@"%d %@", ABS(hour),[NSString stringWithFormat:NSLocalizedString(@"hourago", nil)]];
}
else {
time= [NSString stringWithFormat:@"%d %@", ABS(hour),[NSString stringWithFormat:NSLocalizedString(@"hoursago", nil)]];
if (minute>0) {
time= [NSString stringWithFormat:@"%d %@", ABS(minute),[NSString stringWithFormat:NSLocalizedString(@"minuteago", nil)]];
}
else {
time= [NSString stringWithFormat:@"%d %@", ABS(minute),[NSString stringWithFormat:NSLocalizedString(@"minuteago", nil)]];
if (seconds>0) {
time= [NSString stringWithFormat:@"%d %@", ABS(seconds),[NSString stringWithFormat:NSLocalizedString(@"secondago", nil)]];
}
else {
time= [NSString stringWithFormat:@"%d %@", ABS(seconds),[NSString stringWithFormat:NSLocalizedString(@"secondsago", nil)]];
}
}
}
}
}
return time;
}
///按規定,我們將使用日期格式
我會用'NSTimeInterval''的'而不是''int''。這就是''timeIntervalSince1970''返回的結果,它是一個C''double''。 – amb
:D,不明白你在試圖說什麼 –