iOS中NSDate常用转换操作整合
//当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss
+ (NSString *)getCurrentDataWithDateFormate:(NSString *)formate
{
NSDate *now = [NSDate date];
return [self dateFormattingWithDate:now toFormate:formate];
} //任意NSDate格式化
+ (NSString *)dateFormattingWithDate:(NSDate *)date toFormate:(NSString *)formate
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formate];
return [formatter stringFromDate:date];
} //获取当天0点时间
+ (NSDate *)returnToDay0Clock
{
NSDate *now = [NSDate date];
NSCalendar *calender = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];
int hour = (int)[dateComponent hour];
int minute = (int)[dateComponent minute];
int second = (int)[dateComponent second];
//当前时分秒:hour,minute,second
//返回当前时间(hour * 3600 + minute * 60 + second)之前的时间,即为今天凌晨0点
NSDate *nowDay = [NSDate dateWithTimeIntervalSinceNow: - (hour * + minute * + second)];
long long inter = [nowDay timeIntervalSince1970] * ;
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:inter / ];
return newDate;
} //获取当天24点时间
+ (NSDate *)returnToDay24Clock
{
NSDate *now = [NSDate date];
NSCalendar *calender = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];
int hour = (int)[dateComponent hour];
int minute = (int)[dateComponent minute];
int second = (int)[dateComponent second];
//一天是60分钟 * 60秒 * 24小时 = 86400秒
NSDate *nextDay = [NSDate dateWithTimeIntervalSinceNow: - (hour * + minute * + second) + ];
return nextDay;
} //获取当前秒数
+ (long long)getCurrentDateSecond
{
return [[NSDate date] timeIntervalSince1970];
} //NSDate转秒
+ (long long)dateTosecond:(NSDate *)date
{
return [date timeIntervalSince1970];
} //秒转NSDate
+ (NSDate *)secondToDate:(long long)second
{
return [NSDate dateWithTimeIntervalSince1970:second];
} //是否是12小时制; YES:12小时制 / NO:24小时制
+ (BOOL)is12HourSystem
{
NSString *formatStringForHour = [NSDateFormatter dateFormatFromTemplate:@"j" options: locale:[NSLocale autoupdatingCurrentLocale]];
NSRange contains = [formatStringForHour rangeOfString:@"a"];
BOOL is12HourSystem = contains.location != NSNotFound;
return is12HourSystem;
} //朋友圈/聊天 时间显示样式
+ (NSString *)dateDisplayResult:(long long)secondCount
{
NSDate *date = [self secondToDate:secondCount];
NSCalendar *calender = [NSCalendar currentCalendar];
//判断是否是今天
if ([calender isDateInToday:date]) { long long dateSecondCount = [[NSDate date] timeIntervalSinceDate:date];
if (dateSecondCount < ) {
return @"刚刚";
}
if (dateSecondCount < ( * )) {
return [NSString stringWithFormat:@"%d分钟前",(int)(dateSecondCount / )];
}
return [NSString stringWithFormat:@"%d小时前",(int)(dateSecondCount / ( * ))];
} //判断是否是昨天
NSString *formatterString = @" HH:mm";
if ([calender isDateInYesterday:date]) {
formatterString = [@"昨天" stringByAppendingString:formatterString];
} else {
//判断是否是一年内
formatterString = [@"MM-dd" stringByAppendingString:formatterString];
//判断是否值一年之前
NSDateComponents *component = [calender components:NSCalendarUnitYear fromDate:date toDate:[NSDate date] options:NSCalendarWrapComponents]; if (component.year >= ) {
formatterString = [@"YYYY-" stringByAppendingString:formatterString];
}
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formatterString];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en"];
return [formatter stringFromDate:date];
} //比较两个NsDate对象的时间差
+ (CompareResult *)compareDateDifferenceDate1:(NSDate *)date1 date2:(NSDate *)date2
{
CompareResult *result = [[CompareResult alloc] init];
result.value = (fabs([date1 timeIntervalSinceDate:date2]));
result.trend = [date1 compare:date2];
return result;
}
iOS中NSDate常用转换操作整合的更多相关文章
- iOS中RGB颜色转换
iOS中RGB常用的色值,同时可将对颜色的设置定义成宏,方便开发应用,如: // RGB颜色转换(16进制->10进制) #define UIColorFromRGB(rgbValue) [UI ...
- 大数据(5) - HDFS中的常用API操作
一.安装java 二.IntelliJ IDEA(2018)安装和破解与初期配置 参考链接 1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/d ...
- iOS 中捕获截屏操作
转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...
- python中 列表常用的操作
列表可以装大量的数据,不限制数据类型,表示方式:[]:列表中的元素用逗号隔开. lst = [] #定义一个空列表 lst = ["Tanxu",18,"女", ...
- iOS中的场景转换机制的浅显分析
目前Apple推荐的场景转换的方法有以下几个: 一般的跳转方法: presentViewController Discussion In a horizontally compact environm ...
- iOS中多线程常用的知识点
1.pThread 跨平台的多线程技术 , 是IEEE制定的POSIX 表示可移植性操作系统接口的多线程计数,UNIX内核平台 Unix,Linux,Mac(小红帽) (windows上有可移 ...
- ios中的safari转换时间戳问题
后台下发的时间格式如:2016-09-15 15:30:10:也没多想,直接用new Date('2016-09-15 15:30:10').getTime()获取时间戳:开发时候,电脑调试OK,当放 ...
- iOS中UITextField常用设置和方法
//初始化textField并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; // ...
- 【spark】常用转换操作:reduceByKey和groupByKey
1.reduceByKey(func) 功能: 使用 func 函数合并具有相同键的值. 示例: val list = List("hadoop","spark" ...
随机推荐
- Youtube 视频下载
Youtube 视频下载 由于特殊原因,需要下载 Youtube 的视频. https://www.clipconverter.cc/
- git 修改本地分支名称和远程分支名称
branch-A 为旧分支名称 branch-B 为新分支名称 修改本地分支名称 $ git branch -m branch-A branch-B 删除远程分支 $ git push origin ...
- java代码--------实现位运算符不用乘除法啊
总结:<<:乘法 >>:除法 package com.mmm; public class dfd { public static void main(String[] args ...
- java代码-----------逻辑运算符、 &&逻辑与 ||或
总结: && :两者均满足.是true ||: 两者中有一个满足就为true,不然就是false package com.sads; public class shou { publi ...
- Java 中的this关键字
1.this关键字代表当前对象 this.属性 操作当前对象的属性 this.方法 调用当前对象的方法 2.封装对象的属性的时候,经常会使用this关键字 public class Telphone ...
- Joker的运维开发之路
python 1--数据类型,流程控制 2--数据类型详细操作,文件操作,字符编码 https://mp.weixin.qq.com/s/i3lcIP82HdsSr9LzPgkqww 点开更精彩 目前 ...
- 【UVALive】2678 Subsequence(尺取法)
题目 传送门:QWQ 分析 一开始没看到都是正整数根本不会做...... 看到了就是水题了.(但还是sb WA了一发) 尺取法搞一搞 代码 #include <bits/stdc++.h> ...
- VS 2017 Region快捷键无法折叠
- CORDIC逼近算法
现在开始学习CORDIC算法 学习的博文: (1)http://blog.csdn.net/liyuanbhu/article/details/8458769 三角函数计算,Cordic 算法入门 ...
- nginx的408错误
client_header_timeout:Http核心模块指令,指令指定读取客户端请求头标题的超时时间.这里的超时是指一个请求头没有进入读取步骤,如果连接超过这个时间而客户端没有任何响应,Nginx ...