//当前时间格式化, 例: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常用转换操作整合的更多相关文章

  1. iOS中RGB颜色转换

    iOS中RGB常用的色值,同时可将对颜色的设置定义成宏,方便开发应用,如: // RGB颜色转换(16进制->10进制) #define UIColorFromRGB(rgbValue) [UI ...

  2. 大数据(5) - HDFS中的常用API操作

    一.安装java 二.IntelliJ IDEA(2018)安装和破解与初期配置 参考链接 1.进入官网下载IntelliJ IDEA https://www.jetbrains.com/idea/d ...

  3. iOS 中捕获截屏操作

    转自:iOS知识小集 在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作.苹果的描述如下: // T ...

  4. python中 列表常用的操作

    列表可以装大量的数据,不限制数据类型,表示方式:[]:列表中的元素用逗号隔开. lst = [] #定义一个空列表 lst = ["Tanxu",18,"女", ...

  5. iOS中的场景转换机制的浅显分析

    目前Apple推荐的场景转换的方法有以下几个: 一般的跳转方法: presentViewController Discussion In a horizontally compact environm ...

  6. iOS中多线程常用的知识点

        1.pThread 跨平台的多线程技术 , 是IEEE制定的POSIX 表示可移植性操作系统接口的多线程计数,UNIX内核平台 Unix,Linux,Mac(小红帽) (windows上有可移 ...

  7. ios中的safari转换时间戳问题

    后台下发的时间格式如:2016-09-15 15:30:10:也没多想,直接用new Date('2016-09-15 15:30:10').getTime()获取时间戳:开发时候,电脑调试OK,当放 ...

  8. iOS中UITextField常用设置和方法

    //初始化textField并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; // ...

  9. 【spark】常用转换操作:reduceByKey和groupByKey

    1.reduceByKey(func) 功能: 使用 func 函数合并具有相同键的值. 示例: val list = List("hadoop","spark" ...

随机推荐

  1. 使用dnSpy对目标程序(EXE或DLL)进行反编译修改并编译运行

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 本文使用的工具下载地址为: https://github.com/cnxy/dnSpy/arc ...

  2. Linux 解压 压缩 tar

    tar 格式:tar   [选项]   目录或文件 // 将 test 文件夹打包成 test.tar $ tar cvf test.tar test // 将 home 目录下的 test 文件夹打 ...

  3. MOSS 2013研究系列---列表的资源限制

    MOSS2010 以后,对列表的条目数做了一些限制,大量的将数据存储在列表中,会降低列表的运行效能,因此,MOSS中对列表默认有了一个阀值,默认是5000条数据,当你存储的数据多余5000条的时候,用 ...

  4. POJ1745动态规划

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11622   Accepted: 4178 Des ...

  5. HttpClient使用示例

    1)使用HttpClient发送GET请求 public class MainActivity extends Activity implements OnClickListener { privat ...

  6. Java中 如何把Object类型强转成Map<String, String>类型

    首先你需要保证要转换的Object的实际类型是Map<String, String> 假设Object变量名为obj,强制转换(Map<String, String>)obj ...

  7. 很漂亮的按钮css样式(没有用到图片,可直接拷贝代码使用)

    [转自]http://blog.csdn.net/lushuaiyin/article/details/8118669 对于程序员,有时候也需要对页面风格做些改动,整体的页面风格还是美工的工作. 按钮 ...

  8. 【POJ】3616 Milking Time(dp)

    Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10898   Accepted: 4591 Des ...

  9. 《Effective C++(第三版)》-笔记

    1. 让自己习惯C++ 条款01: 视C++为一个语言联邦 1.1 C++ 是一个多重泛型编程语言(multiparadigm programming),支持:过程形式(procedural),面向对 ...

  10. 转-----FPGA工程师:持守梦想or屈于现实

     昨晚无意间看到一段新闻频道对最近炒得火热的“史上最年轻教授”的专访,倒是他的一位同学对于梦想的“现实版”解说颇有些耐人寻味.大体意思是说“拼了老命考上一所梦寐以求的大学,父母辛辛苦苦交了学费,我们却 ...