objectC时间用法
#define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd HH:mm:ss")
//获取当前日期,时间
+(NSDate *)getCurrentDate{
NSDate *now = [NSDate date];
return now;
}
//将日期转换为字符串(日期,时间)
+(NSString *)getDateStringFromDate:(NSDate *)date{
NSInteger location = 0;
NSString *timeStr = @"";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setDateFormat:@"HH:mm:a"];
NSString *ampm = [[[formatter stringFromDate:date] componentsSeparatedByString:@":"] objectAtIndex:2];
timeStr = [formatter stringFromDate:date];
NSRange range = [timeStr rangeOfString:[NSString stringWithFormat:@":%@",ampm]];
location = range.location;
NSString *string = [timeStr substringToIndex:location];
timeStr = [NSString stringWithFormat:@"%@ %@",ampm,string];
NSString *dateStr = @"";
NSDateFormatter *Dformatter = [[[NSDateFormatter alloc] init] autorelease];
[Dformatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[Dformatter setDateFormat:@"yyyy-MM-dd"];
dateStr = [Dformatter stringFromDate:date];
// NSLog(@"%@", [NSString stringWithFormat:@"%@ %@",dateStr,timeStr]);
return [NSString stringWithFormat:@"%@ %@",dateStr,timeStr];
}
//计算两个日期之间的差距,过了多少天。。
+(NSInteger)getDateToDateDays:(NSDate *)date withSaveDate:(NSDate *)saveDate{
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:date toDate:saveDate options:0];
NSInteger diffDay = [ cps day ];
[chineseClendar release];
return diffDay;
}
例:NSDate *lastDate = [self getSaveDate];//saveDate通过将NSDate转换为NSString来保存
currentDate = [NSDate date];
NSInteger day = [DateHelper getDateToDateDays:currentDate withSaveDate: lastDate];
//日期转字符串
+ (NSString * )NSDateToNSString: (NSDate * )date
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
return dateString;
}
//字符串转日期
+ (NSDate * )NSStringToNSDate: (NSString * )string
{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
NSDate *date = [formatter dateFromString :string];
[formatter release];
return date;
}
//1970年到现在的秒数转换成时间显示
- (NSString *)bySecondGetDate:(NSString *)second{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
NSString *dateLoca = [NSString stringWithFormat:@"%@",second];
NSTimeInterval time=[dateLoca doubleValue];
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [formatter stringFromDate:detaildate];
return timestr;
}
objectC时间用法的更多相关文章
- Sqlserver_时间用法
函数 描述 GETDATE() 返回当前的日期和时间 DATEPART() 返回日期/时间的单独部分 DATEADD() 在日期中添加或减去指定的时间间隔 DATEDIFF() 返回两个日期之间的时间 ...
- cuda中时间用法
转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...
- mysql group_concat时间用法
第一张表的worksId在第二张表中对应多条数据,需要将每条数据的日期作为结果查询出来,一个作为“初审时间”,另一个作为“复审时间”: 可以使用group_concat 和 group by 来进行分 ...
- DevExpress ASPxSplitter ClientSideEvents-PaneExpanded 时间用法
在webform中的devexpress aspxsplitter中套用了jquery-easyui的 tabs id=tt <div id="tt" class=" ...
- 数据库CASE 函数 时间用法
select * from warehouse_trade_detail whereb_createtime>= cast('2016-01-01' as date) and b_createt ...
- php set_time_limit()用法测试详解
在php中set_time_limit函数是用来限制页面执行时间的,如我想把一个php页面的执行时间定义为5秒就可以set_time_limit(5)了. 一个php脚本通过crontab每5分钟 ...
- Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime
一.添加依赖 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thym ...
- c++11之日期和时间库
本文主要介绍 std::chrono日期和时间用法. 演示环境: vs2017 0.头文件 1 #include <chrono> 2 #include <thread>// ...
- C 语言 时间函数使用技巧(汇总)
time.h 头文件 是 C 语言中 有关 时间的函数所储存的头文件 #include <time.h> 在介绍时间函数用法之前,我们首先要了解在 time.h 头文件中已经声明了的一个结 ...
随机推荐
- 【ajax跨域】原因原理解决
1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...
- Python - python不是内部或外部命令
[方法一]我的电脑->属性->高级->环境变量->系统变量 在系统变量里找到PATH,双击PATH,在结尾加上 ";C:\Python26"(不要引号) ...
- iOS常见问题(1)
一.storyboard连线问题 产生原因:将与storyboard关联的属性删除了,但是storyboard中还保持之前所关联的属性. 解决: 1.点击view controller 2.点击这排最 ...
- VS2013中自动缩进和注释的快捷键
自动缩进: ctrl +k 再 ctrl +f 注释: ctrl+k 再 ctrl +c 取消注释: ctrl+k 再 ctrl+u
- 【转】 Android经验: proguard 阻碍 webview 正常工作
转自:http://blog.csdn.net/span76/article/details/9065941 WebView 常识 使用 Alert 提供消息 我在页面经常用 Alert 提供消息, ...
- android 开发自建wifi热点的默认ip
android 开发自建wifi热点的默认ip是:192.168.43.1 (小米3测试)
- [原创汉化]linux前端神器 WebStorm8 汉化
只汉化了linux版本 因为linux的工具没win多 不过汉化应该都通用的,自行尝试下. 汉化的不是很完全.有时间放出完全版本来.汉化是个体力活 转载随易,汉化不易,且转且注明 截图: http:/ ...
- c++ 哪些自定义的数据类型
http://www.cnblogs.com/ShaneZhang/archive/2013/06/21/3147648.html 这些数据类型是 C99 中定义的,具体定义在:/usr/includ ...
- 通过email分享
MFMailComposeViewController *mailC=[[MFMailComposeViewControlleralloc] init]; if ([MFMailComposeView ...
- MyEclipse 安装JRebel进行热部署
安装环境 版本:myeclipse2015stable2 说明:下面是我已经安装了界面 安装过程 进入市场 出现下面提示,不用管它,点Continue 用关键词搜索 配置 进入JRebel配置中心,配 ...