我需要得到可能作为当月,但我做不到.我怎样才能做到这一点.
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day , .Month , .Year], fromDate: date)
let year = components.year
let month = components.month
let day = components.day
Run Code Online (Sandbox Code Playgroud)
我做了这个,但没有奏效.
我想将NSDate转换为NSString?怎么可能?
我尝试过这个,但它没有用(它产生异常):
NSString *dateToString=[[NSString alloc] initWithFormat:(NSString *)dateObj];
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我的应用程序显示消息列表,每个消息都有一个创建日期.
我不想只显示日期,而是根据创建日期过去的时间显示不同的格式.
例如,这是我从日期创建日期字符串的方式:
NSDate *date = someMessage.creationDate;
NSTimeInterval timePassed = [[NSDate date] timeIntervalSinceDate:date];
if (timePassed < 60 ) { // less then one minute
return @"now"
} else if (timePassed < 3600) { // less then one hour
NSInteger minutes = (NSInteger) floor(timePassed / 60.0);
return [NSString stringWithFormat:@"%d minutes ago", minutes];
}
Run Code Online (Sandbox Code Playgroud)
所以现在我想添加以下案例:
else if ("timePassed < 24 hours ago and within the same calendar day")
// do something
} else if ("timePassed > 24 hours, or within previous calendar …Run Code Online (Sandbox Code Playgroud) NSDate/NSDateFormatter 等人似乎为常见任务占用了太多代码.
像DateJS for JavaScript 这样的iOS开发有什么更好的东西吗?
以下是常见NSDate用法的一些示例,其中规范解决方案可以至少两倍小: