您的位置:首页 > 移动开发 > Objective-C

Object-C日期时间与字符串的转化

2011-12-23 11:23 253 查看
字符串转化为日期:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date=[dateFormatter dateFromString:@"2011-12-23 10:55:55"]; //需要转化的字符串

[dateFormatter release];

日期转化为字符串:

NSDateFormatter *dateToStringFormatter=[[NSDateFormatter alloc] init];

[dateToStringFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *nsDate=[dateToStringFormatter stringFromDate:date];

NSLog(@"%@", nsDate);

[dateToStringFormatter release];

转化XML日期格式:

NSString *yourXMLDate = @"Wed, 23 Sep 2009 16:03:03 GMT" ;

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];

[inputFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss 'GMT"];

NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

[outputFormatter setDateFormat:@"yyyy-M-dd, HH:mm a"];

NSString *outputDate = [outputFormatter stringFromDate:inputDate];

NSLog(@"The date is %@", outputDate);

[inputFormatter release];

[outputFormatter release];

具体NSDateFormatter格式如下:引用地址:http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table

Date Field Symbol Table

FieldSym.No.ExampleDescription
era

(纪元)
G1..3ADEra - Replaced with the Era string for the current date. One to three letters for the abbreviated form, four letters for the long form, five for the narrow form.
4Anno Domini
5A
year

(年)
y1..n1996Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length. Example:

Yearyyyyyyyyyyyyyyy
AD 1101001000100001
AD 121212012001200012
AD 12312323123012300123
AD 12341234341234123401234
AD 123451234545123451234512345
Y1..n1997Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year.
u1..n4601Extended year. This is a single number designating the year of this calendar system, encompassing all supra-year fields. For example, for the Julian calendar system, year numbers are positive, with an era of BCE or CE. An extended year value for the Julian
calendar system assigns positive values to CE years and negative values to BCE years, with 1 BCE being year 0.
quarter

(季度)
Q1..202Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3Q2
42nd quarter
q1..202Stand-Alone Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3Q2
42nd quarter
month

(月)
M1..209Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or five for the narrow name.
3Sept
4September
5S
L1..209Stand-Alone Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or 5 for the narrow name.
3Sept
4September
5S
week

(周)
w1..227Week of Year. 在年度中的第几周 这是数字的
W13Week of Month 在月中的第几周
day

(日)
d1..21Date - Day of the month 在月中的第几日
D1..3345Day of year 在年中的第几日
F12

Day of Week in Month. The example is for the 2nd Wed in July
g1..n2451334Modified Julian day. This is different from the conventional Julian day number in two regards. First, it demarcates days at local zone midnight, rather than noon GMT. Second, it is a local number; that is, it depends on the local time zone. It can be thought
of as a single number that encompasses all the date-related fields.
week

day

(周)
E1..3TuesDay of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name.
英文形式的星期
4Tuesday
5T
e1..22Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week.
同E
3Tues
4Tuesday
5T
c12Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the short day, or four for the full name, or five for the narrow name.
3Tues
4Tuesday
5T
period

(上下午)
a1AMAM or PM
hour

(小时)
h1..211Hour [1-12]. 12小时制
H1..213Hour [0-23]. 24小时制
K1..20Hour [0-11].
k1..224Hour [1-24].
minute

(分钟)
m1..259Minute[0~59]. Use one or two for zero padding.
second

(秒)
s1..212Second[0~59]. Use one or two for zero padding.
S1..n3457Fractional Second - rounds to the count of letters. (example is for 12.34567)
毫秒,例子见后面获得毫秒
A1..n69540000Milliseconds in day. This field behaves exactly like a composite of all time-related fields, not including the zone fields. As such, it also reflects discontinuities of those fields on DST transition days. On a day of DST onset, it will jump forward.
On a day of DST cessation, it will jump backward. This reflects the fact that is must be combined with the offset field to obtain a unique local time value.
zone

(时区)
z1..3PDTTimezone - Use one to three letters for the short timezone or four for the full name. For more information, seeAppendix
J: Time Zone Display Names
4Pacific Daylight Time
Z1..3-0800Use one to three letters for RFC 822, four letters for GMT format.
4GMT-08:00
v1PTUse one letter for short wall (generic) time, four for long wall time. For more information, seeAppendix J: Time Zone Display
Names
4Pacific Time
All non-letter character represent themselves in a pattern, except for the single quote. It is used to 'escape' letters. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.

得到当前的日期:

NSDate *date = [NSDate date];

NSLog(@"current date:%@",date);

得到(24 * 60 * 60)即24小时之前的日期:

NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];

NSLog(@"yesterday:%@",yesterday);

获得日期的各个值:

NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];

NSDate *date = [NSDate date];

[formatter setTimeStyle:NSDateFormatterMediumStyle];

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];

NSInteger unitFlags = NSYearCalendarUnit |

NSMonthCalendarUnit |

NSDayCalendarUnit |

NSWeekdayCalendarUnit |

NSHourCalendarUnit |

NSMinuteCalendarUnit |

NSSecondCalendarUnit;

comps = [calendar components:unitFlags fromDate:date];

int week = [comps weekday];

int year=[comps year];

int month = [comps month];

int day = [comps day];

int hour = [comps hour];

int min = [comps minute];

int sec = [comps second];

NSLog(@"week%d",week);

NSLog(@"year%d",year);

NSLog(@"month%d",month);

NSLog(@"day%d",day);

NSLog(@"hour%d",hour);

NSLog(@"min%d",min);

NSLog(@"sec%d",sec);

获得毫秒:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];

NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);

[dateFormatter release];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: