您的位置:首页 > 其它

NSDateFormatter使用

2015-02-09 14:11 155 查看
- (void)dataFormatter
{
   
/*

     NSDateFormatter格式表示:@”YYYY/MM/dd” --> 2011/01/08

     

     @”KK:mm aa” --> 02:51 PM

     以下是setDateFormat可使用的英文代号:

     

     纪元的表示:

     G:表示AD,也就是公元

     

     年的表示:

     yy:年的后面2位数字

     yyyy:表示完整的年

     

     月的表示:

     M:表示成1~12,1位数或2位数

     MM:表示成01~12,不足2位数会补0

     MMM:英文月份的缩写,例如:Jan

     MMMM:英文月份完整表示,例如:January

     

     日期的表示:

     d:表示成1~31,1位数或2位数

     dd:表示成01~31,不足2位数会补0

     

     星期的表示:

     EEE:星期的英文缩写,如Sun

     EEEE:星期的英文完整表示,如,Sunday

     

     上/下午的表示:

     aa:表示AM或PM

     

     小時的表示:

     H:表示成0~23,1位數或2位數(24小時制)

     HH:表示成00~23,不足2位數會補0(24小時制)

     K:表示成0~12,1位數或2位數(12小時制)

     KK:表示成0~12,不足2位數會補0(12小時制)

     

     分的表示:

     m:表示0~59,1位數或2位數

     mm:表示00~59,不足2位數會補0

     

     秒的顯示:

     s:顯示0~59,1位數或2位數

     ss:顯示00~59,不足2位數會補0

     S:
毫秒的顯示

     

     時區的顯示:z / zz /zzz
:PDT

     zzzz:Pacific Daylight Time

     Z / ZZ / ZZZ :-0800

     ZZZZ:GMT -08:00

     v:PT

     vvvv:Pacific Time

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

    //
设置时区
   
NSTimeZone *timeZone = [NSTimeZone
timeZoneWithName:@"Asia/Shanghai"];
    [dateFormatter
setTimeZone:timeZone];
    [dateFormatter
setDateFormat:@"HH:mm"];
// 这里是用大写的 H

    //[dateFormatter setDateFormat:@"KK:mm aa"]; //
时:分
上午

    //
国家  @"en_US" @"zh_CN"
    dateFormatter.locale = [[NSLocale
alloc] initWithLocaleIdentifier:@"en_US"];
   
/*

     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秒

     };

     */

    dateFormatter.dateStyle =
kCFDateFormatterFullStyle;

    dateFormatter.timeStyle =
kCFDateFormatterFullStyle;
   
NSString *HHmm = [dateFormatter
stringFromDate:[NSDate
date]];
   
NSLog(@"%@",HHmm);
// 时间转成字符串

#pragma mark -

    //
字符串转回时间
   
NSDate *date = [dateFormatter
dateFromString:HHmm];

    //
时间戳
   
long reservationTime = (long)[date
timeIntervalSince1970];

#pragma mark -
   
/*

     // 获取系统是24小时制或者12小时制

     NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];

     NSRange containsA = [formatStringForHours rangeOfString:@"a"];

     // hasAMPM==TURE为12小时制,否则为24小时制

     BOOL hasAMPM = containsA.location != NSNotFound;

     */

#pragma mark - 日历

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

    NSDateComponents* components = [[[NSDateComponents
alloc]
init] autorelease];
   
NSInteger unitFlags =
NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSWeekdayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit;
    components = [calendar
components:unitFlags
fromDate:date];
   
NSUInteger week = [components
weekday];
   
/*

     week = [comps weekday];

     month = [comps month];

     day = [comps day];

     hour = [comps hour];

     min = [comps minute];

     sec = [comps second];

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