iOS|iOS NSDate常用方法
//时间戳转时间的方法.时间戳 1368082020. 转 2018/06/23 转换格式自己定
+ (NSString *)getTimeStrByTimeSp:(NSString *)timeStamp format:(NSString *)timeFormat{
//时间戳转时间的方法
timeStamp = [timeStamp substringToIndex:10];
NSTimeInterval time2 =[timeStamp doubleValue];
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:time2];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:timeFormat];
NSString *currentTime = [formatter stringFromDate:date2];
return currentTime;
}
//使用这里把你要格式化的时间格式传给它timeString是时间戳
NSString *timeString = [NSString stringWithFormat:@"%ld",model.examTime];
NSString *dateString = [NSDate getTimeStrByTimeSp:timeString format:@"yyyy/MM/dd"];
【iOS|iOS NSDate常用方法】//服务器时间 1368082020 时间戳 转NSDate
NSString *serviceString = [NSDate getTimeStrByTimeSp:serverTime format:@"YYYY-MM-dd HH:mm:ss"];
NSLocale *CurrenLocale = [NSLocale currentLocale];
NSTimeZone *localZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *serverDate = [NSDate dateWithString:serviceString format:@"YYYY-MM-dd HH:mm:ss" timeZone:localZone locale:CurrenLocale];
```
NSDate分类方法
+ (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
if (timeZone) [formatter setTimeZone:timeZone];
if (locale) [formatter setLocale:locale];
return [formatter dateFromString:dateString];
}
//开始时间
//时间字符串2018-11-16 20:15:33 转NSDate.
NSDate *seckillDate = [NSDate dateWithString:exchangeListInfo.seckillStartTime format:@"YYYY-MM-dd HH:mm:ss" timeZone:localZone locale:CurrenLocale];
//NSDate分类方法
+ (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
if (timeZone) [formatter setTimeZone:timeZone];
if (locale) [formatter setLocale:locale];
return [formatter dateFromString:dateString];
}
//时间相减
NSTimeInterval futureTimeInterval = [seckillDate timeIntervalSinceDate:serverDate];
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- iOS中的Block
- 数组常用方法一
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密