NSDateFormatter调整时间格式的代码

在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理。
例如:
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

//用[NSDate date]可以获取系统当前时间
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

//输出格式为:2010-10-27 10:22:13
NSLog(@”%@”,currentDateStr);

//alloc后对不使用的对象别忘了release
[dateFormatter release];

NSDateComponents

NSDateComponents封装在一个可扩展的,面向对象的方式的日期组件。它是用来弥补时间的日期和时间组件提供一个指定日期:小时,分钟,秒,日,月,年,等等。它也可以用来指定的时间,例如,5小时16分钟。一个NSDateComponents对象不需要定义所有组件领域。当一个NSDateComponents的新实例被创建,日期组件被设置为NSUndefinedDateComponent。

一个NSDateComponents对象本身是毫无意义的;你需要知道它是针对什么日历解释,你需要知道它的值是否是正整数和值是多少。

NSDateComponents的实例不负责回答关于一个日期以外的信息,它是需要先初始化的。例如,如果你初始化一个对象为2004年5月6日,其星期几NSUndefinedDateComponent,不是星期四。要得到正确的星期几,你必须创建一个NSCalendar日历实例,创建一个NSDate对象并使用dateFromComponents:方法,然后使用components:fromDate:检索平周几

Getting Information About an NSDateComponents Object

获取一个NSDateComponents对象的信息

  • – era 时代
  • – year 年
  • – month 月
  • – day 天
  • – hour 时
  • – minute 分
  • – second 秒
  • – week
  • – weekday
  • – weekdayOrdinal
  • – quarter 季度

Setting Information for an NSDateComponents Object

设置一个NSDateComponents对象的信息

  • – setEra:
  • – setYear:
  • – setMonth:
  • – setDay:
  • – setHour:
  • – setMinute:
  • – setSecond:
  • – setWeek:
  • – setWeekday:
  • – setWeekdayOrdinal:
  • – setQuarter:

例子如下:获得2004年5月6日是星期几

NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setDay:6];

[comps setMonth:5];

[comps setYear:2004];

NSCalendar *gregorian = [[NSCalendar alloc]

initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *date = [gregorian dateFromComponents:comps];

[comps release];

NSDateComponents *weekdayComponents =

[gregorian components:NSWeekdayCalendarUnit fromDate:date];

int weekday = [weekdayComponents weekday];

NSLog(@"%d",weekday);

如果需要更加详细,请查阅 “Calendars, Date Components, and Calendar Units” in Date and Time Programming Guide.日期和时间的程序指南

//一段例子:计算距离某一天还有多少时间

NSDate* toDate   = [ [ NSDate alloc] initWithString:@"2012-9-29 0:0:00 +0600" ];

NSDate*  startDate  = [ [ NSDate alloc] init ];

NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];

NSUInteger unitFlags =

NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;

NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:startDate  toDate:toDate  options:0];

NSInteger diffHour = [cps hour];

NSInteger diffMin    = [cps minute];

NSInteger diffSec   = [cps second];

NSInteger diffDay   = [cps day];

NSInteger diffMon  = [cps month];

NSInteger diffYear = [cps year];

NSLog(  @" From Now to %@, diff: Years: %d  Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d",

[toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );

[toDate release];

[startDate release];

[chineseClendar release];

NSDateFormatter调整时间格式的代码的更多相关文章

  1. [IOS 开发] NSDateFormatter的格式字符串 -- 《整理的笔记》

    在ios开发中, OBjective-C中的NSDate是一个挺讨厌的类型, 自己找不到转换成字符串的类型,还得带一个NSDateFormatter的类型. 官方文档上对NSDateFormatter ...

  2. 使用gfortran将数据写成Grads格式的代码示例

    使用gfortran将数据写成Grads格式的代码示例: !-----'Fortran4Grads.f90' program Fortran4Grads implicit none integer,p ...

  3. 让Label等控件支持HTML格式的代码? 使用NSAttributedString:

    > 如何让Label等控件支持HTML格式的代码? 使用NSAttributedString: NSString *htmlString = @"<div>Tate< ...

  4. NSDate时间类/NSDateFormatter日期格式类

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { // NSDate 时间类 继承自N ...

  5. NSDateFormatter 时间格式转换

    NSString *strDate = @“Wed Apr ::”; NSDateFormatter *dateFomatter =[[NSDateFormatter alloc] init]; [d ...

  6. jQuery邮箱格式验证代码

    代码实例如下: <!DOCTYPE html><html><head><meta charset="utf-8"><meta ...

  7. HTML表单基本格式与代码

    咱们先来看下今天咱们需要学习的内容,理解起来很简单,像我这种英语不好的只是需要背几个单词 在HTML中创建表单需要用到的最基本的代码和格式 <form method="post/get ...

  8. PGM图片格式与代码

    这两天在搞神经网络,里面的一个人脸数据库的图片格式是PGM,事实上之前早就知道了这个图片格式,可是没去深究这个图片格式的数据究竟是什么安排的.搜索了下百度百科,发现介绍的真是简单,以下就自己来系统地整 ...

  9. Emu8086三种格式的代码-(顺序,分支,循环)

    这个学期准备考研,于是就没有怎么听别的课,现在临近期末,汇编成了个难题.下面是我学校的实验报告 做一个复习的记录吧,下面的代码都是在Emu8086上运行出来的代码 下面先介绍一下,汇编里面的格式问题 ...

随机推荐

  1. ALAssetsLibrary 照片相关 浅析

    ALAssetsLibrary 提供了访问iOS设备下”照片”应用下所有照片和视频的接口: 从 ALAssetsLibrary 中可读取所有的相册数据,即 ALAssetsGroup 对象列表: 从每 ...

  2. HDU - 2502 Subway

    题目链接:http://poj.org/problem?id=2502 分析: 告诉一些地铁线路,从起点到终点,中途可以步行,可以坐地铁,找一条最短的路 主要是把图建立好,然后直接dijkstra或者 ...

  3. 转:LR性能测试结果样例分析 测试结果分析

    LoadRunner性能测试结果分析是个复杂的过程,通常可以从结果摘要.并发数.平均事务响应时间.每秒点击数.业务成功率.系统资源.网页细分图.Web服务器资源.数据库服务器资源等几个方面分析,如图1 ...

  4. CodeForces 190D Non-Secret Cypher

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  5. LNMPA by lin

    CentOS系统下执行:wget -c http://soft.vpser.net/lnmp/lnmp1.1-full.tar.gz && tar zxf lnmp1.1-full.t ...

  6. Yii config 配置

    Yii2 配置文件 常用配置总结 <?php // 主配置文件 $config = array( 'modules' => array( 'gii' => array( 'class ...

  7. JSON数据传递

    Servlet端代码 try { while (rs.next()) { for(int i=0;i<60;i++){ Day[i]+=rs.getInt("Day"+(i+ ...

  8. ERROR CL .exe……错误

    这个错误遇见了两次,一次是在装VC时,解决办法见 这个 还有一次是VS2012时,遇见了: error MSB6006: “CL.exe”已退出,代码为 -1073741515. 解决办法:Add t ...

  9. Request 地址栏传值

    request页面 protected void btnSearch_Click(object sender, EventArgs e) { Response.Redirect("Reque ...

  10. zf-关于触摸屏子系统(查询机)的页面

    这个页面 一般是 touch/index.jsp 这个要记下来. -- 今天有个任务 是把查询机的UI页面给换掉