IOS --- 日期时间格式 更改

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

NSString* string [email protected]"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 );

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

时间: 2025-02-01 14:51:31

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

IOS --- 日期时间格式 转换

1.如何如何将一个字符串如" 20110826134106"装化为任意的日期时间格式,下面列举两种类型: NSString* string [email protected]"20110826134106"; NSDateFormatter*inputFormatter = [[[NSDateFormatter alloc] init]autorelease]; [inputFormattersetLocale:[[[NSLocale alloc] initWith

Windows2008R2 IIS7日期时间格式更改

背景:最近把WEB程序从Win2003迁移到Win2008R2上,遇到一个问题:页面上日期显示格式跟原来不一样(如图).现在记录一下备忘. 正确格式是:2016-09-13:而显示出来是2016/09/13. 怎么解决此问题呢? 解决方法: 1. 在控制面板----区域和语言----格式----日期和时间格式,变更为如下格式. 2. 区域和语言----管理----复制设置----选中"欢迎屏幕和系统帐户" 3. 服务器管理器----角色----Web服务器(IIS)----系统服务. 

Sql日期时间格式转换

Sql日期时间格式转换 sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-02-01 08:02/*时间一般为getdate()函数或数据表里的字段*/ CONVERT(varchar(10), 时间一, 23) 结果:2007-02-01 /*varchar(10)表示日期输出的格式,如果不够长会发生截取*/ 语句及查询结果:Select CONV

DEDE日期时间格式大全

打造最全的CMS类教程聚合! 日期时间格式 (利用strftime()函数格式化时间) 首页: ([field:pubdate function='strftime("%m-%d",@me)'/])==(5-15) ([field:pubdate function='strftime("%b %d, %Y",@me)'/])==(May 15, 2008) 列表页: [field:pubdate function="GetDateTimeMK(@me)&q

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

一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12-12T00:00:00.000Z 2. 2014-12-12T00:00:00.000+08 3. 2014-12-12T00:00:00.000+0800 4. 2014-12-12T00:00:00.000+08:00 5. 2004-W17-3 6. 0001-165 详细说明请参考度娘:http:/

日期时间格式正则表达式

日期时间格式正则表达式 本文章已收录于: 正则表达式 (?n:^(?=\d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2)(?=.{3,4}(1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|0?[1-9]|1\d|2[0-8])(?<sep>[/.-])(?<month>0?[1-9]|1[012])\2(?

Android日期时间格式国际化

公共类 的DateFormatSymbols 扩展对象 实现 Serializable接口 Cloneable接口 java.lang.Object的    ? java.text.DateFormatSymbols 类概述 封装本地化的日期时间格式的数据,如几个月的名字,一周天的名字,和时区数据 的DateFormat 和 SimpleDateFormat 都使用 的DateFormatSymbols封装此信息. 通常情况下,你应该不能直接使用的DateFormatSymbols.相反,我们鼓

Eclipse 修改注释的 date time 日期时间格式,即${date}变量格式

Eclipse 修改注释的 date time 日期时间格式,即${date}变量格式 找到eclipse安装目录下面的plugins目录,搜索 org.eclipse.text ,找到一个jar包, 例如我找到的jar包为:org.eclipse.text_3.5.300.v20130515-1451.jar 然后打开它,找到这个类: org.eclipse.jface.text.templates.GlobalTemplateVariables 我们重写这个类就行了.(可反编译,也可以找到源

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

dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strftime()函数格式化时间)0 首页: ([field:pubdate function='strftime("%m-%d",@me)'/])==(5-15) ([field:pubdate function='strftime("%b %d, %Y",@me)'/])