iOS 应用程序中的日期和时间格式
Date and Time Format in iOS application
我收到的响应格式低于日期格式
2015-12-23 05:17:04
但是,我想检查日期和时间,例如
date == today ?
date == yesterday ?
time == 1 hour
time == 2 hour till 9 hour
怎么做?
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSString stringWithFormat:@"yyyyMMddHHmmss"];
NSString *now = [dateFormatter stringFromDate:date];
你可以试试上面的代码
祝福
用这个方法给你返回NSString。您可以在方法中比较字符串或时间。
-(NSString* )AgoStringFromTime:(NSDate*)dateTime
{
NSDictionary *timeScale = @{@"sec" :@1,
@"min" :@60,
@"hr" :@3600,
@"day" :@86400,
@"week" :@605800,
@"month":@2629743,
@"year" :@31556926};
NSString *scale;
int timeAgo = 0-(int)[dateTime timeIntervalSinceNow];
if (timeAgo < 60) {
scale = @"sec";
} else if (timeAgo < 3600) {
scale = @"min";
} else if (timeAgo < 86400) {
scale = @"hr";
} else if (timeAgo < 605800) {
scale = @"day";
} else if (timeAgo < 2629743) {
scale = @"week";
} else if (timeAgo < 31556926) {
scale = @"month";
} else {
scale = @"year";
}
timeAgo = timeAgo/[[timeScale objectForKey:scale] integerValue];
NSString *s = @"";
if (timeAgo > 1) {
s = @"s";
}
return [NSString stringWithFormat:@"%d %@%@", timeAgo, scale, s];
}
1.frist 你可以格式化你的字符串到日期,像这样:
+ (NSDate *)dateFromString:(NSString *)dateString {
if (!dateString) {
return nil;
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
[formatter setLocale:[NSLocale currentLocale]];
NSDate *date= [formatter dateFromString:dateString];
return date;
}
2.then,可以使用NSDate,
的方法
+ (instancetype)date;//get current
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;//Returns the interval between the receiver and another given date.
要检查日期是否是今天、昨天、明天等...您可以使用 NSDate-Extensions
例如:
NSDate *date = [self convertStringToDate:@"2015-12-24 12:40:04" formate:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"%d", [date isToday]);
NSLog(@"%d", [date isTomorrow]);
NSLog(@"%d", [date isYesterday]);
- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormat setDateFormat:formate];
return [dateFormat dateFromString:date];
}
我收到的响应格式低于日期格式
2015-12-23 05:17:04
但是,我想检查日期和时间,例如
date == today ? date == yesterday ?
time == 1 hour time == 2 hour till 9 hour
怎么做?
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSString stringWithFormat:@"yyyyMMddHHmmss"];
NSString *now = [dateFormatter stringFromDate:date];
你可以试试上面的代码
祝福
用这个方法给你返回NSString。您可以在方法中比较字符串或时间。
-(NSString* )AgoStringFromTime:(NSDate*)dateTime
{
NSDictionary *timeScale = @{@"sec" :@1,
@"min" :@60,
@"hr" :@3600,
@"day" :@86400,
@"week" :@605800,
@"month":@2629743,
@"year" :@31556926};
NSString *scale;
int timeAgo = 0-(int)[dateTime timeIntervalSinceNow];
if (timeAgo < 60) {
scale = @"sec";
} else if (timeAgo < 3600) {
scale = @"min";
} else if (timeAgo < 86400) {
scale = @"hr";
} else if (timeAgo < 605800) {
scale = @"day";
} else if (timeAgo < 2629743) {
scale = @"week";
} else if (timeAgo < 31556926) {
scale = @"month";
} else {
scale = @"year";
}
timeAgo = timeAgo/[[timeScale objectForKey:scale] integerValue];
NSString *s = @"";
if (timeAgo > 1) {
s = @"s";
}
return [NSString stringWithFormat:@"%d %@%@", timeAgo, scale, s];
}
1.frist 你可以格式化你的字符串到日期,像这样:
+ (NSDate *)dateFromString:(NSString *)dateString {
if (!dateString) {
return nil;
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
[formatter setLocale:[NSLocale currentLocale]];
NSDate *date= [formatter dateFromString:dateString];
return date;
}
2.then,可以使用NSDate,
的方法+ (instancetype)date;//get current
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;//Returns the interval between the receiver and another given date.
要检查日期是否是今天、昨天、明天等...您可以使用 NSDate-Extensions
例如:
NSDate *date = [self convertStringToDate:@"2015-12-24 12:40:04" formate:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"%d", [date isToday]);
NSLog(@"%d", [date isTomorrow]);
NSLog(@"%d", [date isYesterday]);
- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[dateFormat setDateFormat:formate];
return [dateFormat dateFromString:date];
}