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" ...
随机推荐
- 一行代码搞定 FTP 服务
环境搭建: python windows/linux pip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib/ ...
- 20181104_C#线程之Thread_ThreadPool_使用Thread实现回到和带参数的回调
C# .net Framework多线程演变路径: 1.0 1.1 时代使用Thread 2.0 时代使用ThreadPool 3.0 时代使用Task 4.0 时代使用 ...
- Go - 开始
学习Go的缘由 由于LZ目前在使用docker,docker的编程语言使用的是“Go”,所以想更加深入的了解Docker(尝试着看懂source code)遂尝试了解下Golang. 安装 LZ用的是 ...
- 十四 关于interrupt, interrupted, isInterrupted
1 判断线程是否是停止状态? interrupt() : interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位 ...
- POJ-2282题解&数位DP总结
一.题意 给定一个区间[a, b](注意输入的时候可能a > b,所以,在数据输入后,要先比较a和b,如果a > b,交换a和b的值),统计这个区间里面,数位上有多少个0.多少个1.--. ...
- PyQt 5的基本功能
PyQt5常用的模块 PyQt5的类别分为几个模块,包括: QtCore:包含非核心的GUI功能,此模块用于处理时间.文件和目录.各种数据类型.流.URL.MIME类型.线程或进程 QtGui:包括窗 ...
- canvas之太阳系效果
星球 变量名 公转周期 光色 暗色 水星 Mercury 87.70 #a69697 #5c3e40 金星 Venus 224.701.70 #c4bbac #1f1315 地球 Earth 365. ...
- java传递是引用的拷贝,既不是引用本身,更不是对象
java传递是引用的拷贝,既不是引用本身,更不是对象 2008-09-16 04:27:56| 分类: Java SE|举报|字号 订阅 下载LOFTER客户端 1. 简单类型是按值 ...
- CocoStudio资源区导入Plist/PSD文件
这两种文件在使用中和普通文件稍有不同,下作简单介绍.如有不适的地方欢迎批评指正. 首先简单说一下Plist文件,Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息,该功能在旧式的Mac OS ...
- [Cpp primer] range for (c++11)
for (declaration : expression) statement; /* This statement will iterate through the elements in the ...