ios时间戳和日期的一些转换,如具体时间,年龄,星座等等
1、
///将时间戳转为日期时间/// @param timestamp 时间戳/// @param dateFormat 日期样式 YYYY-MM-dd HH:mm:ss+ (NSString *)getTimeFromTimestamp:(NSInteger )timestamp andDateFormat:(NSString *)dateFormat{//将对象类型的时间转换为NSDate类型NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:timestamp];//设置时间格式NSDateFormatter * formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:dateFormat];//将时间转换为字符串NSString *timeStr = [formatter stringFromDate:myDate];return timeStr;}
2、
/// 将时间戳转具体日期如刚刚几分钟之前等/// @param timestamp 时间戳+ (NSString *) timeStr:(NSInteger )timestamp{// 创建日历对象NSCalendar *calendar = [NSCalendar currentCalendar];// 获取当前时间NSDate *currentDate = [NSDate date];// 获取当前时间的年、月、日。利用日历NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:currentDate];NSInteger currentYear = components.year;NSInteger currentMonth = components.month;NSInteger currentDay = components.day;NSInteger currentHour = components.hour;NSInteger currentMinute = components.minute;NSInteger currentSecond = components.second;// 获取消息发送时间的年、月、日NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:timestamp];components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:msgDate];CGFloat msgYear = components.year;CGFloat msgMonth = components.month;CGFloat msgDay = components.day;NSInteger msgHour = components.hour;NSInteger msgMinute = components.minute;NSInteger msgSecond = components.second;// 进行判断NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {//今天if (currentHour == msgHour) {if (currentMinute == msgMinute || currentMinute-1 ==msgMinute) {return langaugeString(@"commonPart", @"just");//刚刚}else if (currentMinute-1 >msgMinute) {return [NSString stringWithFormat:@"%ld%@",(currentMinute*60+currentSecond - msgMinute*60-msgSecond)/60,langaugeString(@"commonPart", @"minutes")];// xx 分钟前}}else if(currentHour-1 == msgHour){//3点10分 1点11分if (currentMinute < msgMinute) {return [NSString stringWithFormat:@"%ld%@",(60*60+currentMinute*60+currentSecond - msgMinute*60-msgSecond)/60,langaugeString(@"commonPart", @"minutes")];// xx 分钟前}else{return [NSString stringWithFormat:@"%ld%@",(currentHour*60+currentMinute - msgHour*60-msgMinute)/60,langaugeString(@"commonPart", @"hours")]; //xx 小时前}}else if(currentHour-1 > msgHour){return [NSString stringWithFormat:@"%ld%@",(currentHour*60+currentMinute - msgHour*60-msgMinute)/60,langaugeString(@"commonPart", @"hours")];//xx 小时前}}else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay&¤tHour<msgHour){return [NSString stringWithFormat:@"%ld%@",(24*60-(msgHour*60+ msgMinute- currentHour*60-currentMinute))/60,langaugeString(@"commonPart", @"hours")];//xx 小时前}else{if (currentYear == msgYear) {dateFmt.dateFormat = @"MM-dd";// MM月dd日}else{dateFmt.dateFormat = @"yyyy-MM-dd";}}// 返回处理后的结果NSString *returnTimeString = [dateFmt stringFromDate:msgDate];return returnTimeString;}
3//获取年龄
+ (NSString *) getAge:(NSString *)birthday {NSDateFormatter*df = [[NSDateFormatter alloc] init];//格式化[df setDateFormat:@"yyyy/MM/dd"];NSString *dateStr = birthday;NSTimeInterval dateDiff = [[df dateFromString:dateStr] timeIntervalSinceNow];long age = fabs(dateDiff/(60*60*24))/365;NSLog(@"年龄是:%@",[NSString stringWithFormat:@"%ld岁",age]);NSString *year = [dateStr substringWithRange:NSMakeRange(0, 4)];NSString *month = [dateStr substringWithRange:NSMakeRange(5, 2)];NSString *day = [dateStr substringWithRange:NSMakeRange(dateStr.length-2, 2)];NSLog(@"出生于%@年%@月%@日", year, month, day);NSDate *nowDate = [NSDate date];NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];NSDateComponents *compomemts = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];NSInteger nowYear = compomemts.year;NSInteger nowMonth = compomemts.month;NSInteger nowDay = compomemts.day;NSLog(@"今天是%ld年%ld月%ld日", nowYear, nowMonth, nowDay);// 计算年龄NSInteger userAge = nowYear - year.intValue - 1;if ((nowMonth > month.intValue) || (nowMonth == month.intValue && nowDay >= day.intValue)) {userAge++;}NSLog(@"用户年龄是%ld",userAge);return [NSString stringWithFormat:@"%ld",(long)userAge];}
4/// 将日期转为时间戳
/// @param CurrentTime 所转的时间
+ (NSString *)getTimestampFromTime:(NSString *)CurrentTime{ZTLog(@"formatTime - - - - - -%@",CurrentTime);NSDateFormatter *formatter = [[NSDateFormatter alloc] init];[formatter setDateStyle:NSDateFormatterMediumStyle];[formatter setTimeStyle:NSDateFormatterShortStyle];[formatter setDateFormat:@"yyyy/MM/dd"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制//设置时区选择北京时间NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];[formatter setTimeZone:timeZone];NSDate* date = [formatter dateFromString:CurrentTime]; //------------将字符串按formatter转成nsdate//时间转时间戳的方法:NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue] * 1000;ZTLog(@"将某个时间转化成 时间戳timeSp:%ld",(long)timeSp); //时间戳的值return [NSString stringWithFormat:@"%ld",(long)timeSp];}
5、/// 根据时间戳得到星座
/// @param birthdayTime 时间戳
+ (NSString *)getConstellatoryByBirthdayTime:(NSTimeInterval )birthdayTime{// 时间戳转为 dateNSDate *birthdayDate = [NSDate dateWithTimeIntervalSince1970:birthdayTime/1000]; // 毫秒时间戳要/1000// 出生日期转换 年月日NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:birthdayDate];NSInteger brithDateMonth = [components1 month];NSInteger brithDateDay = [components1 day];NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";NSString *astroFormat = @"102123444543";NSString *result;if (brithDateMonth < 1||brithDateMonth > 12||brithDateDay < 1||brithDateDay > 31 ){return @"错误日期格式!";}if(brithDateMonth ==2 && brithDateMonth > 29 ){return @"错误日期格式!!";}else if(brithDateMonth == 4 || brithDateMonth == 6 || brithDateMonth == 9 || brithDateMonth == 11) {if (brithDateDay > 30) {return @"错误日期格式!!!";}}result = [NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(brithDateMonth*2-(brithDateDay < [[astroFormat substringWithRange:NSMakeRange((brithDateMonth-1), 1)] intValue] - (-19))*2,2)]];return result;/*白羊座: 3月21日~4月20日 (Aries)金牛座: 4月21日~5月21日 (Taurus)双子座: 5月22日~6月21日 (Gemini)巨蟹座: 6月22日~7月22日 (Cancer)狮子座: 7月23日~8月23日 (Leo)处女座: 8月24日~9月23日 (Virgo)天秤座: 9月24日~10月23日 (Libra)天蝎座: 10月24日~11月22日 (Scorpio)射手座: 11月23日~12月21日 (Sagittarius)摩羯座: 12月22日~1月20日 (Capricorn)水瓶座: 1月21日~2月19日 (Aquarius)双鱼座: 2月20日~3月20日 (Pisces)*/}
/// 获取当前时间的时间戳
+ (NSString *)getNowTimeTimestamp{NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;[formatter setDateStyle:NSDateFormatterMediumStyle];[formatter setTimeStyle:NSDateFormatterShortStyle];[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制//设置时区,这个对于时间的处理有时很重要NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];[formatter setTimeZone:timeZone];NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];return timeSp;}
ios时间戳和日期的一些转换,如具体时间,年龄,星座等等相关推荐
- php date hi,php中时间戳和日期格式的转换
原文:php中时间戳和日期格式的转换 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2 ...
- mysql13位的时间戳怎么转化_MySQL时间函数 | 时间戳和日期之间得转换
首页 专栏 mysql 文章详情 0 MySQL时间函数 | 时间戳和日期之间得转换 阿壮Jonsson 发布于 2 月 6 日 一.时间戳转日期 select FROM_UNIXTIME(16060 ...
- timestamp 转换 date mysql_MySQL时间函数 | 时间戳和日期之间得转换
一.时间戳转日期select FROM_UNIXTIME(1606028010, '%Y-%m-%d %H:%i:%s'); 二.日期转时间戳select unix_timestamp('2018-0 ...
- php用什么服务器系统时间格式,php中时间戳和日期格式的转换
一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2009-1-22″) 结果:12325 ...
- mysql 13位时间戳转日期_MySQL时间函数 | 时间戳和日期之间得转换
一.时间戳转日期select FROM_UNIXTIME(1606028010, '%Y-%m-%d %H:%i:%s'); 二.日期转时间戳select unix_timestamp('2018-0 ...
- 【Java】时间戳与日期之间的转换
获取时间戳: long s=System.currentTimeMillis(); long s1=new Date().getTime(); long s2=Calendar.getInstance ...
- mysql转换戳转换成小时_MySQL时间函数 | 时间戳和日期之间得转换
一.时间戳转日期select FROM_UNIXTIME(1606028010, '%Y-%m-%d %H:%i:%s'); 二.日期转时间戳select unix_timestamp('2018-0 ...
- js时间戳与日期格式的转换
1.将时间戳转换成日期格式: function timestampToTime(timestamp) {// 时间戳为10位需*1000,时间戳为13位不需乘1000var date = new Da ...
- MySQL时间戳与日期格式的相互转换
MySQL时间戳与日期格式的相互转换,PHP时间戳与日期格式的相互转换 MySQL: 获取当前时间 SELECT NOW(); // 2018/10/11 14:22:51 时间日期格式转换成时间戳格 ...
最新文章
- 图文并茂,万字详解,带你掌握 JVM 垃圾回收!
- 高薪Java工程师必看的书籍
- EJB继承与Java继承不同
- AI开发者福音!阿里云推出国内首个基于英伟达NGC的GPU优化容器
- 小程序分享到朋友圈_微信内测开放小程序分享到朋友圈功能
- physx选择显卡还是cpu_99块钱买啥显卡?PUBG吃鸡60fps+的缩水版“GTX1050”3GB游戏实测...
- 威纶触摸屏使用说明书_「西门子1200PLC教程」20.PLC变量表的使用
- 计算机三级之嵌入式系统学习笔记9
- # Please enter the commit message for your changes. Lines starting # with ‘#‘ will be ignored
- 自己实现一个SQL解析引擎
- left join 最后一条_一条Mysql查询语句的西天取经之路,你真的了解吗?
- 问题五十一:怎么用ray tracing画tear drop
- Mysql--Auto_increment详解
- 题目448-寻找最大数
- 苹果电脑计算机找不到打印机,macbook air电脑关于添加打印机的问题
- 使用傲梅分区软件删除U盘分区后U盘无法识别的解决方法
- unity隐藏鼠标光标的2种方法
- 中国各地区政府招投标数据
- 20190919CF训练
- 旧电脑没有usb boot 启动选项,有没有光驱如何重装系统。