我正在尝试创建一个重新排列dateString顺序的函数。我有一系列的新闻日期,我基本上想检查它们是从今天还是昨天。 dateString如下所示:
2015-03-23 18:58:00
所以如果他们今天我想要这样的输出:
Today, 16:30
如果他们来自昨天
Yesterday, 16:30
如果它们不是今天或者是日期,它应该是
1. April, 16:30
到目前为止,我已经创建了这个函数,它只有currentDate和newsDate。我怎样才能比较它们以获得上述逻辑?
func getDate(dateStr:String, format:String = "yyyy-MM-dd HH:mm:ss") -> String {
//news DateTime
var dateFmt = NSDateFormatter()
dateFmt.timeZone = NSTimeZone.defaultTimeZone()
dateFmt.dateFormat = format
let newsDateTime = dateFmt.dateFromString(dateStr)!
//currentDate
let date = NSDate();
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = NSTimeZone()
let localDate = dateFormatter.stringFromDate(date)
//newsDate
let newsDateFmt = NSDateFormatter()
newsDateFmt.dateFormat = "yyyy-MM-dd"
let newsDate = newsDateFmt.stringFromDate(newsDateTime)
let toStringDate = NSDateFormatter()
return toStringDate.stringFromDate(newsDateTime)
}
答案 0 :(得分:0)
根据当前日期时间检查各个日期组件。
this thread的可能重复?
答案 1 :(得分:0)
首先将日期与系统日期进行比较。
if {date
等于system date
)
NSLog("Today");
从日期开始获取日期并进行比较,并在昨天之前查看日期
在我的一个项目中查看我比较日期。
NSArray *arrSortDate = [arrDate sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
NSDate *d1 = [formatter dateFromString:obj1[@"key"]];
NSDate *d2 = [formatter dateFromString:obj2[@"key"]];
return [d2 compare:d1];
}];
此处arrDate
是NSMutableArray。它有效。
答案 2 :(得分:0)
我将在Objective-C中回答,因为我确信你可以从中提取它,这里有一种方法,如果给定的日期早于今天,今天正好,或者一周之后会有不同的反应。现在,或月或年。
+ (NSString*)cellDateFormatWithDate:(NSDate*)date{
NSCalendarUnit units = NSDayCalendarUnit | NSWeekOfYearCalendarUnit |
NSMonthCalendarUnit | NSYearCalendarUnit;
// if `date` is before "now" (i.e. in the past) then the components will be positive
NSDateComponents *components = [[NSCalendar currentCalendar] components:units
fromDate:date
toDate:[NSDate date]
options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
if (components.year > 0) {
[formatter setDateFormat:@"dd/MM/yy"];
} else if (components.month > 0) {
[formatter setDateFormat:@"dd/MM/yy"];
} else if (components.weekOfYear > 0) {
[formatter setDateFormat:@"dd/MM HH:mm"];
} else if (components.day > 0) {
if (components.day > 1) {
[formatter setDateFormat:@"dd/MM HH:mm"];
} else {
[formatter setDateFormat:@"dd/MM HH:mm"];
}
} else {
[formatter setDateFormat:@"HH:mm"];
}
NSString *cellDate = [formatter stringFromDate:date];
return cellDate;
}
要符合您的问题,您只需添加
即可NSString* relativeString;
并在每个if/else
中相应地更改
relativeString = @"Today";
relativeString = @"Yesterday";
relativeString = @"A week ago";
等等。
这种方法几乎完全相同,但精确度为秒/分钟
+ (NSString *)dateDiff:(NSString *)origDate{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *convertedDate = [df dateFromString:origDate];
NSDate *todayDate = [NSDate date];
double ti = [convertedDate timeIntervalSinceDate:todayDate];
ti = ti * -1;
if(ti < 1) {
return NSLocalizedString(@"REL_TIME_NOW", nil);
} else if (ti < 60) {
return NSLocalizedString(@"REL_TIME_LESS_THAN_MINUTE", nil);
} else if (ti < 3600) {
int diff = round(ti / 60);
if (diff < 2){
return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_MINUTE", nil), diff];
}else{
return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_MINUTES", nil), diff];
}
} else if (ti < 86400) {
int diff = round(ti / 60 / 60);
if (diff < 2){
return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_HOUR", nil), diff];
}else{
return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_HOURS", nil), diff];
}
} else {
int diff = round(ti / 60 / 60 / 24);
if (diff < 2){
return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_DAY", nil), diff];
}else{
return [NSString stringWithFormat:NSLocalizedString(@"REL_TIME_DAYS", nil), diff];
}
}
}
虽然我无法重命名所有本地化字符串,但只显示
"less than a minute ago"
"%d minute ago"
"%d minutes ago"
"%d hour ago
&#34; 等等