IOS 時(shí)間和時(shí)間戳之間轉(zhuǎn)化示例
以毫秒為整數(shù)值的時(shí)間戳轉(zhuǎn)換
時(shí)間戳轉(zhuǎn)化為時(shí)間NSDate
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
// 格式化時(shí)間
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
// 毫秒值轉(zhuǎn)化為秒
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
NSString* dateString = [formatter stringFromDate:date];
return dateString;
}
時(shí)間轉(zhuǎn)化為時(shí)間戳
// 當(dāng)前時(shí)間
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精確到毫秒,不乘就是精確到秒
NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //轉(zhuǎn)為字符型
通過(guò)比較時(shí)間與當(dāng)前時(shí)間返回年月日的方法
- (void)getBabyDetailAge:(NSString *)date
{
// 獲得日期對(duì)象
NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];
formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createDate = [formatter_ dateFromString:date];
NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];
NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ];
NSInteger years = [components year];
NSInteger months = [components month ];
NSInteger days = [components day ];
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS UIScrollView和控制器返回手勢(shì)沖突解決方法
這篇文章主要介紹了iOS UIScrollView和控制器返回手勢(shì)沖突解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
舉例講解iOS開(kāi)發(fā)中拖動(dòng)視圖的實(shí)現(xiàn)
這篇文章主要介紹了舉例講解iOS開(kāi)發(fā)中的拖動(dòng)視圖實(shí)現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
解決移動(dòng)端 ios 系統(tǒng)鍵盤(pán)遮擋的問(wèn)題
下面小編就為大家分享一篇解決移動(dòng)端 ios 系統(tǒng)鍵盤(pán)遮擋的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
iOS小技能之字典轉(zhuǎn)模及對(duì)象相等性示例詳解
這篇文章主要為大家介紹了iOS小技能之字典轉(zhuǎn)模及對(duì)象相等性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
IOS中的target action控件的實(shí)現(xiàn)
這篇文章主要介紹了IOS中的target action控件的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)現(xiàn)target action的簡(jiǎn)單實(shí)例幫助大家學(xué)習(xí)理解該如何實(shí)現(xiàn),需要的朋友可以參考下2017-08-08

