#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. UnitTest

    using Bll; using Model; using Dal; using NUnit.Framework; using NUnit.Mocks; using System.ServiceMod ...

  2. linux创建线程之pthread_create

    说明:本文转自多线程编程之pthread_create函数应用,在此基础上笔者做了些许改动. pthread_create函数 函数简介 pthread_create是UNIX环境创建线程函数 头文件 ...

  3. 修复jquery.treeview的增加子节点的方法的bug

    1.修复理由 在一个android项目中用到了treeview控件(本来自己通过android的原生api实现了一个http://www.cnblogs.com/Mr-Nobody/p/3527688 ...

  4. 设置google搜索打开链接时在新标签页显示

    百度的搜索结果,打开链接都会在新的页面打开,但是google却直接在本页面打开,有时候我们打开的不一定是自己想要结果,又习惯性的把当前页面给关掉了......这只是习惯问题,可能国人有这个习惯.怎么设 ...

  5. 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  6. int a[5]={1,2,3,4,5}; int *p=(int*)(&a+1); printf("%d",*(p-1)); 答案为什么是5?

    这个问题的关键是理解 &a a是一个数组名,也就是数组的首地址.对a进行取地址运算符,得到的是一个指向数组的指针!!!!这句话尤为重要!也就相当于int (*p) [5] = &a;p ...

  7. 01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响

    这篇文章 http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id One o ...

  8. Unity3D脚本中文系列教程(五)

    http://dong2008hong.blog.163.com/blog/static/4696882720140302848544/?suggestedreading&wumii Unit ...

  9. linux源代码阅读笔记 free_page_tables()分析

    /* 77 * This function frees a continuos block of page tables, as needed 78 * by 'exit()'. As does co ...

  10. js正则函数match、exec、test、search、replace、split使用介绍

    match() 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) stringObj 必选项.对其进行查找的 String 对象或字符 ...