IOS格式規範

來源:互聯網
上載者:User

標籤: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格式規範

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.