1、怎样怎样将一个字符串如“ 20110826134106”装化为随意的日期时间格式。以下列举两种类型:

   NSString* string =@"20110826134106";

    NSDateFormatter*inputFormatter = [[[NSDateFormatter alloc] init]autorelease];



    [inputFormattersetLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];

    [inputFormattersetDateFormat:@"yyyyMMddHHmmss"];

    NSDate*inputDate = [inputFormatter dateFromString:string];

    NSLog(@"date= %@", inputDate);

    

    NSDateFormatter*outputFormatter = [[[NSDateFormatter alloc] init]autorelease]; 

    [outputFormattersetLocale:[NSLocale currentLocale]];

    [outputFormattersetDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];

    NSString*str = [outputFormatter stringFromDate:inputDate];

    NSLog(@"testDate:%@",str);

两次打印的结果为:

    date= 2011-08-26 05:41:06 +0000

    testDate:2011年08月26日13时41分06秒



2、iOS-NSDateFormatter 格式说明:

G: 公元时代,比如AD公元

    yy:年的后2位

    yyyy:完整年

    MM:月,显示为1-12

    MMM:月。显示为英文月份简写,如 Jan

    MMMM:月。显示为英文月份全称。如 Janualy

    dd:日,2位数表示,如02

    d:日,1-2位显示。如 2

    EEE:简写星期几。如Sun

    EEEE:全写星期几,如Sunday

    aa:上下午。AM/PM

    H:时,24小时制,0-23

    K:时,12小时制,0-11

    m:分,1-2位

    mm:分,2位

    s:秒,1-2位

    ss:秒,2位

    S:毫秒



经常使用日期结构:

yyyy-MM-dd HH:mm:ss.SSS

yyyy-MM-dd HH:mm:ss

yyyy-MM-dd

MM dd yyyy


3:新浪微博接口中,取得日期字符串 的转换

NSString*dateStr = @"Wed May 222:27:08+0800 2012";

NSDateFormatter* formater =[[NSDateFormatter alloc] init];

[formater setDateFormat:@"EEE MMM d HH:mm:sszzzz yyyy"];

    NSDate* date = [formaterdateFromString:dateStr];

    NSLog(@"%@",date);

   

    //NSDate* now = [NSDatenow];

    double inter = fabs([datetimeIntervalSinceNow]);

    if( inter <60)

       NSLog(@"1 mins ago!");

    else if(inter< 60*60)

       NSLog(@"1 hours ago!");

    else if(inter< 60*60*24)

       NSLog(@"1 days ago!");

       NSLog(@"interval
is %f min", inter/60);

上面代码在真机上执行后。发现取得date为NULL,模拟器正常显示。上网搜索后发现须要设置local, 果然设置后,真机正常

NSLocale* local =[[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] autorelease];

[formatter setLocale: local];

4、自己定义显示的 星期 格式

使用NSDateFormatter转换日期时,得到的英文字母的星期几仅仅能是这样,如Sun, Mon, etc.

假设想得到大写字母的星期几,能够这样:

NSArray*weekdayAry = [NSArray arrayWithObjects:@"SUN",
@"MON", @"TUE",@"WED", @"THU", @"FRI", @"SAT", nil];

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:NSLocalizedString(@"YYYY.MM.dd.eee",nil)];

//此处更改显示的大写字母的星期几

   [dateFormattersetShortWeekdaySymbols:weekdayAry];

[dateFormatter setLocale:[[NSLocale
alloc]initWithLocaleIdentifier:@"en_US"] ]];

NString *str= [dateFormatter stringFromDate:[NSDate
date]];

5、计算距离某一天还有多少时间

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

NSDate*  startDate  = [ [ NSDatealloc] init ];

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

NSUInteger unitFlags =

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

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

NSInteger diffHour = [cps hour];

NSInteger diffMin    = [cpsminute];

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 );

版权声明:本文博主原创文章,博客,未经同意不得转载。

IOS --- 日期时间格式 更改的更多相关文章

  1. Win2008 IIS7日期时间格式更改最简便方法

    windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法. windows2008默认时间格 ...

  2. Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()

    一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...

  3. sql 日期时间格式转换

    Sql日期时间格式转换   sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, ...

  4. db2 日期时间格式

    db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...

  5. SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  6. SQL Server日期时间格式转换字符串

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  7. 一起Polyfill系列:让Date识别ISO 8601日期时间格式

    一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...

  8. WPF-数据绑定:日期时间格式

    WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...

  9. 调用DEDE日期时间格式整理大全

    dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...

随机推荐

  1. 在Windows系统上实现轻量级的线程间及进程间消息队列

    Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段. 利用Windows的Naming Object可以实现一套简单的Inter-Thread ...

  2. CSS3 Media Query实现响应Web设计(宽度为不同的移动设备)

    现在的屏幕分辨率.小到320px(iPhone),大到2560px或甚至更高(大显示屏).范围内变化很大.除了使用传统的台式机.用户会越来越多的通过手机.上网本.iPad一类的平板设备来浏览页面. 这 ...

  3. cocos2d-x2.2.5 + cocos2d-x3.2鸟跳便宜源代码“开源”

    尊重开发人员的劳动成果,转载请注明From郝萌主 游戏简单介绍: 贱鸟跳跳,贱贱的小鸟这次遇上大问题了.被它整蛊过的同类都在找它的麻烦,如今我们赶紧到游戏中帮帮它吧!左右手互撸,合理操控.获得高分,打 ...

  4. [置顶] Firefox OS 学习——manifest.webapp结构分析

    在Firefox OS 学习——Gaia 编译分析  这篇文章多次提到manifest.webapp文件,对于做过android app 开发的人来说,都很熟悉Android.mk 和Manifest ...

  5. 基于Redis Sentinel的Redis集群(主从Sharding)高可用方案(转)

    本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...

  6. Maven, Ivy, Grape, Gradle, Buildr, SBT, Leiningen, ant

    Maven, Ivy, Grape, Gradle, Buildr, SBT, Leiningen, ant

  7. 代码重构 & 代码中的坏味道

    1.重构 1.1 为什么要重构 1.1.1 改进程序设计 程序员为了快速完成任务,在没有完全理解整体架构之前就开始写代码, 导致程序逐渐失去自己的结构.重构则帮助重新组织代码,重新清晰的体现 程序结构 ...

  8. Learning Cocos2d-x for WP8(3)——文字篇

    原文:Learning Cocos2d-x for WP8(3)--文字篇 C#兄弟篇Learning Cocos2d-x for XNA(3)——文字篇 文字,是人类文明的象征. 文字显示,可用字符 ...

  9. xamarin跨平台iOS、Android 与Windows

    http://xamarin.csdn.net/ Xamarin是一个行动App开发平台,提供跨平台开发能力,开发人员透过Xamarin开发工具与程序语言,即可开发出iOS.Android 与Wind ...

  10. Maven--403权限问题解决方式(求解决)

    我的程序配置方案例如以下(大牛们相信你们不仅仅是一个传说): 目的:实现maven公布项目到tomcat以下.用eclipse一步到位调试. pom.xml配置: <plugin> < ...