標籤:des style io ar os 使用 sp for strong
IOS格式規範
目錄
概述
日期格式
聲明時間格式:NSDateFormatter *date_formatter = [[NSDateFormatter alloc] init];
設值時間格式:[date_formatter setDateFormat:@"yyyy年MM月dd日HH時mm分ss秒"];
設值locale:[date_formatter setLocale:[[NSLocale alloc] initWithLocaleIndentifier:@"en_US"]]或者[date_formatter setLocale:[NSLocale currentLocale]];
獲得日期
獲得當前日期:NSDate *current_date = [NSDate date];
從字串中獲得日期:NSDate *custom_date = [date_formatter stringFromDate:@"20140905133212"];
把獲得日期按照相應格式轉換為字串:NSString *str = [date_formatter stringFromDate:current_date];
NSDateFormatter格式說明
G:公元時代,例如AD公元
yy:年的後2位
yyyy:完整年
MM:月,顯示為1-12
MMM:月,顯示為英文月份簡寫,如Jan
MMMM:月,顯示為英文月份全稱,如January
dd:日,2位元表示,如2
d:日,1-2位顯示,如2
EEE:簡寫星期幾,如Sun
EEEE:全寫星期幾,如Sunday
aa:上下午,AM/PM
H:時,24小時制,0-23
K:時,12小時制,0-11
m:分,1-2位
mm:分,2位
s:秒,1-2位
ss:秒,2位
S:毫秒
常用日期結構
yyyy-MM-dd HH:mm:ss.sss
yyyy_MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy
自訂顯示的星期格式
使用NSDateFormatter轉換日期時,得到的英文字母的星期幾隻能是這樣,如Sun, Mon, etc.
如果想得到大寫字母的星期幾,可以這樣:
NSArray*weekdayAry = [NSArray arrayWithObjects:@"SUN", @"MON", @"TUE",@"WED", @"THU", @"FRI", @"SAT", nil];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSLocalizedString(@"YYYY.MM.dd.eee",nil)];
//此處更改顯示的大寫字母的星期幾?[dateFormattersetShortWeekdaySymbols:weekdayAry];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] ]];
NString *str= [dateFormatter stringFromDate:[NSDate date]];
計算距離某一天還有多少時間
NSDate* toDate = [ [ NSDate alloc]initWithString:@"2012-9-29 0:0:00 +0600" ];
NSDate* startDate = [ [ NSDatealloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ]initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags =
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [chineseClendar components:unitFlagsfromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [cps hour];
NSInteger diffMin = [cpsminute];
NSInteger diffSec = [cps second];
NSInteger diffDay = [cps day];
NSInteger diffMon = [cps month];
NSInteger diffYear = [cps year];
NSLog( @" From Now to %@, diff: Years:%d Months: %d, Days; %d, Hours: %d, Mins:%d,sec:%d",
[toDate description], diffYear, diffMon, diffDay, diffHour,diffMin,diffSec );
IOS格式規範