时间与时间戳的相互转换以及获取当前设备时间
来源:互联网 发布:js div背景颜色 编辑:程序博客网 时间:2024/06/05 06:13
1.时间转成时间戳
2.时间戳转成时间
NSDate* date = [NSDatedateWithTimeIntervalSince1970: message.receivedTime/1000.0];
NSString* dateString = [NSStringstringWithFormat:@"%@",date];
NSArray *a = [dateStringcomponentsSeparatedByString:@" "];
groupCell.time.text = [a[1]substringToIndex:[a[1]length] -3];
NSDate* date = [NSDatedateWithTimeIntervalSince1970: message.receivedTime/1000.0];
NSTimeZone *zone = [NSTimeZonesystemTimeZone];
NSInteger interval = [zonesecondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
NSString* dateString = [NSStringstringWithFormat:@"%@",localeDate];
NSArray *a = [dateStringcomponentsSeparatedByString:@" "];
groupCell.time.text = [a[1]substringToIndex:[a[1]length] -3];
3. 获取当前系统时间并设置格式
NSDate *date = [NSDatedate];//获得时间对象
NSDateFormatter *forMatter = [[NSDateFormatteralloc]init];
[forMatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateStr = [forMatterstringFromDate:date];
根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
- - (NSString *)distanceTimeWithBeforeTime:(double)beTime
- {
- NSTimeInterval now = [[NSDatedate]timeIntervalSince1970];
- double distanceTime = now - beTime;
- NSString * distanceStr;
- NSDate * beDate = [NSDatedateWithTimeIntervalSince1970:beTime];
- NSDateFormatter * df = [[NSDateFormatteralloc]init];
- [df setDateFormat:@"HH:mm"];
- NSString * timeStr = [df stringFromDate:beDate];
- [df setDateFormat:@"dd"];
- NSString * nowDay = [df stringFromDate:[NSDate date]];
- NSString * lastDay = [df stringFromDate:beDate];
- if (distanceTime < 60) {//小于一分钟
- distanceStr = @"刚刚";
- }
- else if (distanceTime <60*60) {//时间小于一个小时
- distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
- }
- else if(distanceTime <24*60*60 && [nowDayintegerValue] == [lastDay integerValue]){//时间小于一天
- distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
- }
- else if(distanceTime<24*60*60*2 && [nowDayintegerValue] != [lastDay integerValue]){
- if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDayintegerValue] > 10 && [nowDayintegerValue] == 1)) {
- distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
- }
- else{
- [df setDateFormat:@"MM-dd HH:mm"];
- distanceStr = [df stringFromDate:beDate];
- }
- }
- else if(distanceTime <24*60*60*365){
- [df setDateFormat:@"MM-dd HH:mm"];
- distanceStr = [df stringFromDate:beDate];
- }
- else{
- [df setDateFormat:@"yyyy-MM-dd HH:mm"];
- distanceStr = [df stringFromDate:beDate];
- }
- return distanceStr;
- }
- 时间与时间戳的相互转换以及获取当前设备时间
- golang获取当前时间、时间戳和时间字符串及它们之间的相互转换
- 时间戳与时间的相互转换
- 时间与时间戳的相互转换
- 时间与时间戳的相互转换
- Swift - 获取当前时间的时间戳(时间戳与时间互相转换)
- 时间戳和字符串相互转换,获取当前正确的时间
- 【JS】获取当前时间,时间与时间戳之间的转换
- JS获取当前时间,时间与时间戳之间的转换
- java中date和时间戳相互转换以及获取前一个小时的时间
- 【java】获取时间以及相互转换
- JavaScript 时间戳与时间相互转换
- iOS时间与时间戳相互转换
- javascript里面获取当前时间以及与当前时间相差天数的时间
- JS获取当前时间戳的方法转换时间戳
- Python时间戳与时间,时间与时间戳的相互转换
- 时间戳与日期的相互转换
- js 时间格式与时间戳的相互转换
- 第十一天:集成工具eclipse的使用
- 食物链
- 循环队列的相关条件和公式
- codeforces 699 A. Launch of Collider
- Session::Run() 分布式版本
- 时间与时间戳的相互转换以及获取当前设备时间
- Graph 图模块 (UML视图)
- Graph 图模块 —— Graph Loading
- CALayer4-自定义层
- Graph 图构建 (Graph Constructor)
- Tomcat生命周期(Tomcat源码阅读系列之三)
- Graph 图优化 (graph optimizer)
- Java中的Collection和Map详解
- Simple Placer节点布放算法