NSDateFormatter格式详细列表一览
转自:http://www.cnblogs.com/xinus/archive/2012/10/29/NSDateFormatter_samples.html
前言:iOS开发中NSDateFormatter是一个很常用的类,用于格式化NSDate对象,支持本地化的信息。与时间相关的功能还可能会用到NSDateComponents类和NSCalendar类等。本文主要列出NSDateFormatter常见用法。
NSDate对象包含两个部分,日期(Date)和时间(Time)。格式化的时间字符串主要也是针对日期和时间的。[以下代码中开启了ARC,所以没有release。]
1、基础用法

1 NSDate* now = [NSDate date];
2 NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
3 fmt.dateStyle = kCFDateFormatterShortStyle;
4 fmt.timeStyle = kCFDateFormatterShortStyle;
5 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
6 NSString* dateString = [fmt stringFromDate:now];
7 NSLog(@"%@", dateString);

打印输出:10/29/12, 2:27 PM
这使用的系统提供的格式化字符串,通过 fmt.dateStyle 和 fmt.timeStyle 进行的设置。实例中使用的参数是 kCFDateFormatterShortStyle,此外还有:

typedef CF_ENUM(CFIndex, CFDateFormatterStyle) { // date and time format styles
kCFDateFormatterNoStyle = 0, // 无输出
kCFDateFormatterShortStyle = 1, // 10/29/12, 2:27 PM
kCFDateFormatterMediumStyle = 2, // Oct 29, 2012, 2:36:59 PM
kCFDateFormatterLongStyle = 3, // October 29, 2012, 2:38:46 PM GMT+08:00
kCFDateFormatterFullStyle = 4 // Monday, October 29, 2012, 2:39:56 PM China Standard Time
};

2. 自定义区域语言
如上实例中,我们使用的是区域语言是 en_US,指的是美国英语。如果我们换成简体中文,则代码是:
1 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
则对应的输出为:

typedef CF_ENUM(CFIndex, CFDateFormatterStyle) { // date and time format styles
kCFDateFormatterNoStyle = 0, // 无输出
kCFDateFormatterShortStyle = 1, // 12-10-29 下午2:52
kCFDateFormatterMediumStyle = 2, // 2012-10-29 下午2:51:43
kCFDateFormatterLongStyle = 3, // 2012年10月29日 GMT+0800下午2时51分08秒
kCFDateFormatterFullStyle = 4 // 2012年10月29日星期一 中国标准时间下午2时46分49秒
};

世界通用的区域语言代码,详见 International Components for Unicode (ICU), http://userguide.icu-project.org/formatparse/datetime
3. 自定义日期时间格式
NSDateFormatter提供了自定义日期时间的方法,主要是通过设置属性 dateFormat,常见的设置如下:
1 NSDate* now = [NSDate date];
2 NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
3 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
4 fmt.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
5 NSString* dateString = [fmt stringFromDate:now];
6 NSLog(@"%@", dateString);
打印输出:2012-10-29T16:08:40
除了上面列出的,还可以指定很多格式,详见http://userguide.icu-project.org/formatparse/datetime。
结合设置Locale,还可以打印出本地化的字符串信息。
1 NSDate* now = [NSDate date];
2 NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
3 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
4 fmt.dateFormat = @"yyyy-MM-dd a HH:mm:ss EEEE";
5 NSString* dateString = [fmt stringFromDate:now];
6 NSLog(@"\n%@", dateString);
打印输出:2012-10-29 下午 16:25:27 星期一
4. 自定义月份星期等字符
NSDateFormatter中同样提供了相应的方式,去修改这些字符。一般情况下,使用相应区域语言下面的默认字符就OK了。但是你的确有这个需求,那么也是可以办到的。相应的方法非常多,如下:
Managing AM and PM Symbols
- – AMSymbol
- – setAMSymbol:
- – PMSymbol
- – setPMSymbol:
Managing Weekday Symbols
- – weekdaySymbols
- – setWeekdaySymbols:
- – shortWeekdaySymbols
- – setShortWeekdaySymbols:
- – veryShortWeekdaySymbols
- – setVeryShortWeekdaySymbols:
- – standaloneWeekdaySymbols
- – setStandaloneWeekdaySymbols:
- – shortStandaloneWeekdaySymbols
- – setShortStandaloneWeekdaySymbols:
- – veryShortStandaloneWeekdaySymbols
- – setVeryShortStandaloneWeekdaySymbols:
Managing Month Symbols
- – monthSymbols
- – setMonthSymbols:
- – shortMonthSymbols
- – setShortMonthSymbols:
- – veryShortMonthSymbols
- – setVeryShortMonthSymbols:
- – standaloneMonthSymbols
- – setStandaloneMonthSymbols:
- – shortStandaloneMonthSymbols
- – setShortStandaloneMonthSymbols:
- – veryShortStandaloneMonthSymbols
- – setVeryShortStandaloneMonthSymbols:
Managing Quarter Symbols
- – quarterSymbols
- – setQuarterSymbols:
- – shortQuarterSymbols
- – setShortQuarterSymbols:
- – standaloneQuarterSymbols
- – setStandaloneQuarterSymbols:
- – shortStandaloneQuarterSymbols
- – setShortStandaloneQuarterSymbols:
Managing Era Symbols
- – eraSymbols
- – setEraSymbols:
- – longEraSymbols
- – setLongEraSymbols:
NSDateFormatter格式详细列表一览的更多相关文章
- Response.ContentType 详细列表 <转>
Response.ContentType 详细列表 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% ...
- SQL Server 2008 R2 性能计数器详细列表(三)
原文:SQL Server 2008 R2 性能计数器详细列表(三) SQL Server,Deprecated Features 对象: 监视指定为不推荐使用的功能: SQL Server Depr ...
- Response.ContentType 详细列表-请求的内容类型详细记录
Response.ContentType 详细列表-请求的内容类型详细记录 作者:王春天一.应用实例: Response.Clear(); Response.ContentType = "t ...
- 十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧)
十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧) Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱, ...
- WinXP系统服务详细列表
windows XP 系统服务“关闭”详细列表,释放N多内存,128也够用了! 在xp系统中,有近90个服务,默认开启了 30多个服务,而事实上我们只需要其中几个就够用了.禁止所有不必要的服务可以为您 ...
- Oracle_Database_11g_标准版_企业版__下载地址_详细列表
Oracle_Database_11g_标准版_企业版__下载地址_详细列表 Oracle Database 11g Release 2 Standard Edition and Enterprise ...
- SQL Server 2008 R2 性能计数器详细列表(五)
原文:SQL Server 2008 R2 性能计数器详细列表(五) SQL Server:SQL Statistics 对象: 监视编译和发送到 SQL Server 实例的请求类型 SQL Ser ...
- SQL Server 2008 R2 性能计数器详细列表(四)
原文:SQL Server 2008 R2 性能计数器详细列表(四) SQL Server Latches 对象: 监视称为闩锁的内部 SQL Server 资源锁.通过监视闩锁来确定用户活动和资源使 ...
- SQL Server 2008 R2 性能计数器详细列表(二)
原文:SQL Server 2008 R2 性能计数器详细列表(二) SQL Server Buffer Partition 对象: 提供计数器来监视 SQL Server 如何使用可用页 SQL S ...
随机推荐
- 『题解』Codeforces121A Lucky Sum
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Petya loves lucky numbers. Everybody k ...
- windows备份mysql数据库
1.编写执行文件mysql_backup.bat rem auther:ELSON ZENGrem date:20191104rem mysql backup! @echo off set mysql ...
- 花一天时间试玩vsphere6.7(EXSI)服务器版的vmware
花一天时间试玩vsphere6.7(EXSI)服务器版的vmware 要注册账号(2019年11月14注册): 登陆网址:https://my.vmware.com/cn/group/vmware/h ...
- 微擎框架商业版 V2.1.2 去后门一键安装版+去除云平台+无附带模块
下载地址:http://dd.ma/AdVvoDu5 关注微信公众号codervip,点击公众号菜单,获取提取码! 这个是一键安装版本,所以微擎安装比较简单,不用大家手动去改数据库了,而且修复上个2. ...
- lufylegend.js教程(1)
1.图片元素如何缩小? 在LSprite类中,有两个属性:{scaleX,scaleY},这两个属性属于按比例缩放精灵对象,可以放大,可以缩小,注意这两个属性是在图片中心点位置开始缩放. 代码: Bo ...
- python:爬虫0
什么是网页爬虫,也叫网页蜘蛛.把互联网比作一个蜘蛛网,有好多节点,这个蜘蛛在网上爬来爬去,对对网页中的每个关键字进行建立索引,然后建立索引数据库,经过复杂的排序算法后,这些算法的结果将按照相关度的高低 ...
- Pashmak and Parmida's problem(树状数组)
题目链接:http://codeforces.com/contest/459/problem/D 题意: 数列A, ai表示 i-th 的值, f(i,j, x) 表示[i,j]之间x的数目, 问:当 ...
- 使用sklearn和caffe进行逻辑回归 | Brewing Logistic Regression then Going Deeper
原文首发于个人博客https://kezunlin.me/post/c50b0018/,欢迎阅读! Brewing Logistic Regression then Going Deeper. Bre ...
- W5500设计方案
W5500是韩国一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,W5500同时也是一颗工业级以太网控制芯片,最近发现我们国内也有和W5500 芯片一样芯片 介绍给大家 如下图:
- Flex调用本地文件分析
最近在用Flex做一个相册的功能,因为图片数据很多,所以想调用本地文件的方式做. 但是B/S的缘故,很多安全上的限制给我造成了不小的麻烦,把我这个小菜鸟弄的晕头转向. 第一,刚开始,查了很多资料发现都 ...