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. [每日一题] 11gOCP 1z0-052 :2013-09-5 runInstaller oracle of no swap

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11186995 正确答案:A 我们先来看下面这张截图,这是我在安装Oracle 11.2.0 ...

  2. [docker]coreOS与atomic对照

    声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 摘自https://majo ...

  3. Android源代码分析-资源载入机制

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/23387079 (来自singwhatiwanna的csdn博客) 前言 我们 ...

  4. WM_NCHITTEST有21种取值,常用的有HTCAPTION,HTCLIENT,HTBORDER,HTSYSMENU,HTTRANSPARENT,罗列所有VCL里对其使用的情况

    我为了移动一个无标题栏的窗体,使用了WM_NCHITTEST消息,这个消息大概如下: 通常,我们拖动对话框窗口的标题栏来移动窗口,但有时候,我们想通过鼠标在客户区上拖动来移动窗口. 一个容易想到的方案 ...

  5. Linux与JVM的内存关系分析(转)

    引言 在一些物理内存为8g的服务器上,主要运行一个Java服务,系统内存分配如下:Java服务的JVM堆大小设置为6g,一个监控进程占用大约600m,Linux自身使用大约800m.从表面上,物理内存 ...

  6. DecimalFormat

    public class TestDemo { public static void main(String[] args) { String format = new DecimalFormat(& ...

  7. hdu4280(最大流)

    传送门:Island Transport 题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿. 分析:无向图正反都加弧,权值一样,这题点多, ...

  8. xcode6 iOS sdk8.1隐藏系统状态栏

    在代码项目(uzplayer)从iOS6升级到iOS8之后,头发如今视频播放器有.系统状态栏后面的背景: 这样就会导致有的时候按下Donebutton,或者拖滑块没有效果 所以,我们须要想个办法.把这 ...

  9. Android 网络通信框架Volley基本介绍

    Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...

  10. hdu2295(重复覆盖+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2295 题意::一个国家有n个城市,有m个地方可以建造雷达,最多可以建K个雷达(K>=1 & ...