Created
July 27, 2012 09:43
-
-
Save tsbob/3187137 to your computer and use it in GitHub Desktop.
string to date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; | |
[dateFormat setDateFormat:@"yyyy-MM"]; | |
NSDate *date = [dateFormat dateFromString:@"2012-4"]; | |
//1、字符串转换为日期 | |
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象 | |
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式 | |
NSDate *date =[dateFormat dateFromString:@"1980-01-01 00:00:01"]; | |
//2、日期转换为字符串 | |
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象 | |
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式 | |
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]]; | |
//3、字符串转int | |
Convert NSString to int | |
NSString *aNumberString = @"123"; | |
int i = [aNumberString intValue]; | |
//4、int转字符串 | |
Convert int to NSString | |
int aNumber = 123; | |
NSString *aString = [NSString stringWithFormat:@"%d", aNumber]; | |
/* | |
官方文档上对NSDateFormatter的格式串好像没详讲, 或许有,我没找到, 每次使用都是用谷歌摸索. | |
有幸找到一份比较全的文档, 翻译过来共享: | |
a: AM/PM (上午/下午) | |
A: 0~86399999 (一天的第A微秒) | |
c/cc: 1~7 (一周的第一天, 周天为1) | |
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写) | |
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼) | |
d: 1~31 (月份的第几天, 带0) | |
D: 1~366 (年份的第几天,带0) | |
e: 1~7 (一周的第几天, 带0) | |
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写) | |
EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼) | |
F: 1~5 (每月的第几周, 一周的第一天为周一) | |
g: Julian Day Number (number of days since 4713 BC January 1) 未知 | |
G~GGG: BC/AD (Era Designator Abbreviated) 未知 | |
GGGG: Before Christ/Anno Domini 未知 | |
h: 1~12 (0 padded Hour (12hr)) 带0的时, 12小时制 | |
H: 0~23 (0 padded Hour (24hr)) 带0的时, 24小时制 | |
k: 1~24 (0 padded Hour (24hr) 带0的时, 24小时制 | |
K: 0~11 (0 padded Hour (12hr)) 带0的时, 12小时制 | |
L/LL: 1~12 (0 padded Month) 第几月 | |
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 月份简写 | |
LLLL: January/February/March/April/May/June/July/August/September/October/November/December 月份全称 | |
m: 0~59 (0 padded Minute) 分钟 | |
M/MM: 1~12 (0 padded Month) 第几月 | |
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec | |
MMMM: January/February/March/April/May/June/July/August/September/October/November/December | |
q/qq: 1~4 (0 padded Quarter) 第几季度 | |
qqq: Q1/Q2/Q3/Q4 季度简写 | |
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter 季度全拼 | |
Q/QQ: 1~4 (0 padded Quarter) 同小写 | |
QQQ: Q1/Q2/Q3/Q4 同小写 | |
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter 同小写 | |
s: 0~59 (0 padded Second) 秒数 | |
S: (rounded Sub-Second) 未知 | |
u: (0 padded Year) 未知 | |
v~vvv: (General GMT Timezone Abbreviation) 常规GMT时区的编写 | |
vvvv: (General GMT Timezone Name) 常规GMT时区的名称 | |
w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) 一年的第几周, 一周的开始为周日,第一周从去年的最后一个周日起算 | |
W: 1~5 (0 padded Week of Month, 1st day of week = Sunday) 一个月的第几周 | |
y/yyyy: (Full Year) 完整的年份 | |
yy/yyy: (2 Digits Year) 2个数字的年份 | |
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year) 这个年份未知干嘛用的 | |
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year) 这个年份未知干嘛用的 | |
z~zzz: (Specific GMT Timezone Abbreviation) 指定GMT时区的编写 | |
zzzz: (Specific GMT Timezone Name) Z: +0000 (RFC 822 Timezone) 指定GMT时区的名称 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment