#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时间用法的更多相关文章

  1. Sqlserver_时间用法

    函数 描述 GETDATE() 返回当前的日期和时间 DATEPART() 返回日期/时间的单独部分 DATEADD() 在日期中添加或减去指定的时间间隔 DATEDIFF() 返回两个日期之间的时间 ...

  2. cuda中时间用法

    转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...

  3. mysql group_concat时间用法

    第一张表的worksId在第二张表中对应多条数据,需要将每条数据的日期作为结果查询出来,一个作为“初审时间”,另一个作为“复审时间”: 可以使用group_concat 和 group by 来进行分 ...

  4. DevExpress ASPxSplitter ClientSideEvents-PaneExpanded 时间用法

    在webform中的devexpress aspxsplitter中套用了jquery-easyui的 tabs id=tt <div id="tt" class=" ...

  5. 数据库CASE 函数 时间用法

    select * from warehouse_trade_detail whereb_createtime>= cast('2016-01-01' as date) and b_createt ...

  6. php set_time_limit()用法测试详解

    在php中set_time_limit函数是用来限制页面执行时间的,如我想把一个php页面的执行时间定义为5秒就可以set_time_limit(5)了.   一个php脚本通过crontab每5分钟 ...

  7. Thymeleaf 如何支持java8的时间LocalDate和LocalDatetime

    一.添加依赖 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thym ...

  8. c++11之日期和时间库

    本文主要介绍 std::chrono日期和时间用法. 演示环境: vs2017 0.头文件 1 #include <chrono> 2 #include <thread>// ...

  9. C 语言 时间函数使用技巧(汇总)

    time.h 头文件 是 C 语言中 有关 时间的函数所储存的头文件 #include <time.h> 在介绍时间函数用法之前,我们首先要了解在 time.h 头文件中已经声明了的一个结 ...

随机推荐

  1. 命令行插入含有中文的sql文件,报错ERROR 1366 (HY000): Incorrect stringvalue:

    --以下是插入语句: insert into sms_inbox values('123456','123456', 'cd', sysdate(), '今天天 气很好', 1, sysdate(), ...

  2. mysql5.5 Replication 主从同步

    mysql5.5 Replication 主从同步 ------------------[主]------------------[mysqld]server-id=1 log-bin=mysql-b ...

  3. 问题 K: 【USACO2012Feb】植草 {Bronze题2}

    按着矩形周长的思路,到当前边的时候,前一层的覆盖数乘以高度加入 ans 就行,然而真正的算法可能并不是这个..只能想到这个了 ; type node=record l,r,mid,sum,delta: ...

  4. Notes of the scrum meeting(10/30)

    meeting time:9:30~11:30p.m.,October 29th,2013 meeting place:20公寓楼前 attendees: 顾育豪                    ...

  5. android 讯飞语音识别(离线)注意事项

    讯飞语音识别:使用注意事项:mainfest.xml中一定要记得权限写进去21001:我的情况是没有写SpeechApp类,并且需要在application中注册:20005:无匹配结果23300:本 ...

  6. SVN--下载、安装VisualSVN server 服务端和 TortoiseSVN客户端

    前言: 在http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html的博客中已经很详细地介绍了SVN的服务器--VisualS ...

  7. 2014 Multi-University Training Contest 7

    官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uzd4.html Magical Forest http://acm.hdu.edu.cn/sho ...

  8. 2014 Multi-University Training Contest 6

    官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uz2v.html Apple Tree http://acm.hdu.edu.cn/showpro ...

  9. vs-ps combination error

    http://social.msdn.microsoft.com/Forums/en-US/5dfef3d9-edc1-4006-9e81-9d5326419df8/d3d10effect-compi ...

  10. uva 10330 最大流

    拆点  将节点 i 的容量拆成从 i 到 i+n 的边的容量 套用最大流模板 ac #include <cstdio> #include <cstdlib> #include ...