IOS --- 日期时间格式 更改
1、怎样怎样将一个字符串如“ 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
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 --- 日期时间格式 更改的更多相关文章
- Win2008 IIS7日期时间格式更改最简便方法
windows2008 这么高级的系统不可能改个系统的日期时间显示格式还要进注册表啊.于是有baidu,google了下终于发现了,原来还有不需要注册表的更简便方法. windows2008默认时间格 ...
- Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()
一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...
- sql 日期时间格式转换
Sql日期时间格式转换 sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, ...
- db2 日期时间格式
db2日期和时间常用汇总 1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2 ...
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- SQL Server日期时间格式转换字符串
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- 一起Polyfill系列:让Date识别ISO 8601日期时间格式
一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...
- WPF-数据绑定:日期时间格式
WPF-数据绑定:日期时间格式绑定后自定义格式的例子. 我刚才遇到的问题是绑定完之后,星期始终显示为英文.需要一个属性ConverterCulture制定区域. 如下: {Binding dateti ...
- 调用DEDE日期时间格式整理大全
dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...
随机推荐
- wake_lock_timeout的用法
今天实用到用ec43_GPIO的中断来唤醒系统,将系统从深度休眠中唤醒并保证系统wakup 一段时间用过了.方法例如以下.有相同使用的童鞋能够參考一下. 1. 定义一人局部静态变量ec43_wlo ...
- Android资源管理框架(Asset Manager)简介和学习计划
Android该应用程序包括两个部分组成的:代码和资源. 资源主要是与UI相关的东西,例如UI布局.和其他字符串和照片.代码和资源可以使独立的应用程序来组织的实际需求的基础上,在执行的时候UI.,就能 ...
- Delphi 的绘图功能(29篇博客)
http://www.cnblogs.com/del/category/123038.html
- VCL改变主窗体的方法
使用如下语句即可Pointer((@Application.MainForm)^) := Form1; 仔细想想和Pointer((Application.MainForm)) := Form1;有什 ...
- java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)
java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...
- ZOJ3805:Machine
In a typical assembly line, machines are connected one by one. The first machine's output product wi ...
- Android 的独特shell命令
Android本来就是一个linux操作系统,所以大部分都是linux的命令,如mkdir,ls,netstat,mount,ps 等,这里就不具体介绍了, 主要介绍几个Android特有的. get ...
- UVAlive 2519 Radar Installation (区间选点问题)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- hdu 4620 Fruit Ninja Extreme(状压+dfs剪枝)
对t进行从小到大排序(要记录ID),然后直接dfs. 剪枝的话,利用A*的思想,假设之后的全部连击也不能得到更优解. 因为要回溯,而且由于每次cut 的数目不会超过10,所以需要回溯的下标可以利用一个 ...
- hdu1690 Bus System (dijkstra)
Problem Description Because of the huge population of China, public transportation is very important ...