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
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];
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
Field | Sym. | No. | Example | Description | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
era (纪元) | G | 1..3 | AD | Era - 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. | ||||||||||||||||||||||||||||||||||||
4 | Anno Domini | |||||||||||||||||||||||||||||||||||||||
5 | A | |||||||||||||||||||||||||||||||||||||||
year (年) | y | 1..n | 1996 | Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length. Example:
| ||||||||||||||||||||||||||||||||||||
Y | 1..n | 1997 | Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year. | |||||||||||||||||||||||||||||||||||||
u | 1..n | 4601 | Extended 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 (季度) | Q | 1..2 | 02 | Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name. | ||||||||||||||||||||||||||||||||||||
3 | Q2 | |||||||||||||||||||||||||||||||||||||||
4 | 2nd quarter | |||||||||||||||||||||||||||||||||||||||
q | 1..2 | 02 | Stand-Alone Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name. | |||||||||||||||||||||||||||||||||||||
3 | Q2 | |||||||||||||||||||||||||||||||||||||||
4 | 2nd quarter | |||||||||||||||||||||||||||||||||||||||
month (月) | M | 1..2 | 09 | Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or five for the narrow name. | ||||||||||||||||||||||||||||||||||||
3 | Sept | |||||||||||||||||||||||||||||||||||||||
4 | September | |||||||||||||||||||||||||||||||||||||||
5 | S | |||||||||||||||||||||||||||||||||||||||
L | 1..2 | 09 | Stand-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. | |||||||||||||||||||||||||||||||||||||
3 | Sept | |||||||||||||||||||||||||||||||||||||||
4 | September | |||||||||||||||||||||||||||||||||||||||
5 | S | |||||||||||||||||||||||||||||||||||||||
week (周) | w | 1..2 | 27 | Week of Year. 在年度中的第几周 这是数字的 | ||||||||||||||||||||||||||||||||||||
W | 1 | 3 | Week of Month 在月中的第几周 | |||||||||||||||||||||||||||||||||||||
day (日) | d | 1..2 | 1 | Date - Day of the month 在月中的第几日 | ||||||||||||||||||||||||||||||||||||
D | 1..3 | 345 | Day of year 在年中的第几日 | |||||||||||||||||||||||||||||||||||||
F | 1 | 2 | Day of Week in Month. The example is for the 2nd Wed in July | |||||||||||||||||||||||||||||||||||||
g | 1..n | 2451334 | Modified 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 (周) | E | 1..3 | Tues | Day of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name. 英文形式的星期 | ||||||||||||||||||||||||||||||||||||
4 | Tuesday | |||||||||||||||||||||||||||||||||||||||
5 | T | |||||||||||||||||||||||||||||||||||||||
e | 1..2 | 2 | Local 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 | |||||||||||||||||||||||||||||||||||||
3 | Tues | |||||||||||||||||||||||||||||||||||||||
4 | Tuesday | |||||||||||||||||||||||||||||||||||||||
5 | T | |||||||||||||||||||||||||||||||||||||||
c | 1 | 2 | Stand-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. | |||||||||||||||||||||||||||||||||||||
3 | Tues | |||||||||||||||||||||||||||||||||||||||
4 | Tuesday | |||||||||||||||||||||||||||||||||||||||
5 | T | |||||||||||||||||||||||||||||||||||||||
period (上下午) | a | 1 | AM | AM or PM | ||||||||||||||||||||||||||||||||||||
hour (小时) | h | 1..2 | 11 | Hour [1-12]. 12小时制 | ||||||||||||||||||||||||||||||||||||
H | 1..2 | 13 | Hour [0-23]. 24小时制 | |||||||||||||||||||||||||||||||||||||
K | 1..2 | 0 | Hour [0-11]. | |||||||||||||||||||||||||||||||||||||
k | 1..2 | 24 | Hour [1-24]. | |||||||||||||||||||||||||||||||||||||
minute (分钟) | m | 1..2 | 59 | Minute[0~59]. Use one or two for zero padding. | ||||||||||||||||||||||||||||||||||||
second (秒) | s | 1..2 | 12 | Second[0~59]. Use one or two for zero padding. | ||||||||||||||||||||||||||||||||||||
S | 1..n | 3457 | Fractional Second - rounds to the count of letters. (example is for 12.34567) 毫秒,例子见后面获得毫秒 | |||||||||||||||||||||||||||||||||||||
A | 1..n | 69540000 | Milliseconds 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 (时区) | z | 1..3 | PDT | Timezone - Use one to three letters for the short timezone or four for the full name. For more information, seeAppendix J: Time Zone Display Names | ||||||||||||||||||||||||||||||||||||
4 | Pacific Daylight Time | |||||||||||||||||||||||||||||||||||||||
Z | 1..3 | -0800 | Use one to three letters for RFC 822, four letters for GMT format. | |||||||||||||||||||||||||||||||||||||
4 | GMT-08:00 | |||||||||||||||||||||||||||||||||||||||
v | 1 | PT | Use one letter for short wall (generic) time, four for long wall time. For more information, seeAppendix J: Time Zone Display Names | |||||||||||||||||||||||||||||||||||||
4 | Pacific Time |
得到当前的日期:
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];
相关文章推荐
- JS中字符串转化为日期然后比较时间先后
- iOS中日期和字符串的相互转化(时间戳)
- Object-C时间与字符串的转化
- Object-C时间与字符串的转化 因多语言设置中造成返回Nil的解决方法
- js 求时间差 字符串转化为日期
- 新浪旧事-JS中字符串转化与日期的相互转化及比较时间先后
- javascript转换日期字符串---转化为Date对象---再转化为时间戳
- JS中字符串转化为日期然后比较时间先后
- Java将字符串转化为时间格式,与实现日期的计算
- jsp sql server 2000的时间日期格式与字符串相转化
- 后台获取日期年、月、日;日期字符串与时间戳相互转化
- JS中字符串转化与日期的相互转化及比较时间先后
- 小程序中的时间日期 与 字符串的转化
- java parse 带英文单词的日期字符串 转 date (转化新浪微博api返回的时间)
- JS中字符串转化为日期然后比较时间先后
- python 时间字符串与日期转化
- GOLang time 时间戳、日期字符串相互转化
- java parse 带英文单词的日期字符串(转化新浪微博api返回的时间)
- 爪哇国新游记之三十一----日期时间与字符串间的转化
- JS中字符串转化为日期然后比较时间先后