2017年4月27日
一.时间戳和时间相互转换函函数
1.实现
+ (NSString *)dateWithTimeIntervalStr:(NSString *)timeIntervalStr
{
NSString *result = @"";
if ([timeIntervalStr length] > 0) {
// 格式化时间
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone systemTimeZone];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// 毫秒值转化为秒
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeIntervalStr doubleValue] / 1000.0];
result = [formatter stringFromDate:date];
}
return result;
}
+ (NSString *)timeIntervalWithDateStr:(NSString *)dateStr
{
NSString *result = @"";
if ([dateStr length] > 0 && ![dateStr isEqualToString:@"null"]) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [formatter dateFromString:dateStr];
NSTimeInterval a = [date timeIntervalSince1970] * 1000; // *1000 是精确到毫秒,不乘就是精确到秒
result = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
}
return result;
}
2.使用
NSString *temp = [kUserDefaults objectForKey:@"launchTime"];
NSString *launchTime = [HuConfigration timeIntervalWithDateStr:temp]; //[kUserDefaults objectForKey:@"launchTime"];
temp = [HuConfigration timeIntervalWithDateStr:[kUserDefaults objectForKey:@"exitTime"]];
NSString *exitTime = [temp length] > 0 ? temp :@"null";
二. 获取当前时间戳的秒数(用于时间比较)
1.实现
+ (NSInteger)getCurrentSeconds
{
NSDate* nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval nowTimeInterval = [nowDate timeIntervalSince1970]; //默认是s
NSInteger nowTime = (NSInteger)[[NSString stringWithFormat:@"%.0f",nowTimeInterval] doubleValue];
// NSLog(@"wy=%ld",nowTime);
return nowTime;
}
2.使用
//答题剩余时间
NSInteger remainTime;
BOOL isAbsolute = YES;
if (isAbsolute == NO) {
remainTime = [allDataDic[kExamRemainingTimeKey] integerValue];
}else{
NSInteger absoluteTime = [_allDataDic[kExamAbsoluteTimeKey] integerValue];
NSInteger nowTime = [HuConfigration getCurrentSeconds];
remainTime = nowTime>absoluteTime ? 0 :(absoluteTime-nowTime);
}
2017年3月11日
一.补位处理
//不足两位补零处理
NSString *hour = [NSString stringWithFormat:@"%02ld",[d hour]];
NSString *minute = [NSString stringWithFormat:@"%02ld",[d minute]];
NSString *second = [NSString stringWithFormat:@"%02ld",[d second]];
二.返回前几天的时间字符串
- (NSString *)getLasNDate:(int)n
{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow: - (24 * 60 * 60*n)];
[formatter setDateFormat:@"yyyyMMdd"];
NSString *datestr = [NSString stringWithFormat:@"%@", [formatter stringFromDate:date]];
return datestr;
}
三.返回当前时间
+ (NSDateComponents *)getCurrentDate
{
// 获取代表公历的NSCalendar对象
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 获取当前日期
NSDate* dt = [NSDate date];
// 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
// 获取不同时间字段的信息
NSDateComponents* comp = [gregorian components: unitFlags
fromDate:dt];
return comp;
}
1.使用效果
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。