- 獲取當(dāng)天星期,月份颓芭,年份
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *now = [NSDate date];
NSDateComponents *componets = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday ;
componets = [calendar components:unitFlags fromDate:now];
NSInteger weekday = [componets weekday];
- 將NSDate 轉(zhuǎn)換為 NSString
// 將NSDate 中的數(shù)據(jù)提取出來彻犁,組成字符串
// 第一步:日期對(duì)象
NSDate * nowDate = [NSDate date];
// 第二步:創(chuàng)建NSDateFormatter對(duì)象
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
// 第三步:設(shè)定日期轉(zhuǎn)換的格式(提起數(shù)據(jù)的方式)
[formatter setDateFormat:@"yyyy/MM/dd"]; //yyyy 年 MM 月 dd 日
// eeee星期 MMMM大寫月份 QQQQ季度 zzzz時(shí)區(qū) hh:mm:ss 時(shí):分:秒
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss eeee MMMM QQQQ zzzz" ];
// 第四步:轉(zhuǎn)換
NSString * timeStr = [formatter stringFromDate:nowDate];
NSLog(@"%@",timeStr);
- 將NSString轉(zhuǎn)換為NSDate:將NSString中包含的與日期有關(guān)的信息提取出來,創(chuàng)建NSDate對(duì)象
// 第一步:包含信息的字符串
NSString * timeStr = @"2014年05月01日 10點(diǎn)23分18秒";
// 第二步:創(chuàng)建NSDateFormatter對(duì)象
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
// 第三步:設(shè)置日期轉(zhuǎn)換格式(必須匹配)
[formatter setDateFormat:@"yyyy年MM月dd日 HH點(diǎn)mm分ss秒"];
//[formatter setDateFormat:@"yyyy年MM月dd日 hh點(diǎn)mm分ss秒"];
// 第四步:轉(zhuǎn)換
// NSDate * date = [formatter dateFromString:timeStr];
NSDate * date = [formatter dateFromString:timeStr];
NSLog(@"%@", date);
- -------------計(jì)算時(shí)間間隔 在新聞時(shí)候應(yīng)該可以用到--------------
NSDate * newTime = [NSDate date];
NSString * timeStr = @"2015-12-12 00:30:20";
NSDateFormatter * formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate * date = [formater dateFromString:timeStr];
NSTimeInterval inter = [newTime timeIntervalSinceDate:date];
if (inter < 60 ) {
NSLog(@"剛剛");
} else if (inter < 3600 && inter > 60){
inter /= 60;
NSLog(@"%f分鐘前",inter);
} else if (inter > 3600 ){
inter /= 3600;
NSLog(@"%f小時(shí)前",inter);
}
- 獲取當(dāng)前的時(shí)間
// 下面的第一個(gè)方法不提倡
// NSDate *now1 = [[NSDate alloc]initWithTimeIntervalSinceNow:8*60*60];
// NSLog(@"now %@",now1);
//
NSDate * today = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:today];
NSDate *localeDate = [today dateByAddingTimeInterval:interval];
// 時(shí)間轉(zhuǎn)換成時(shí)間戳
NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[localeDate timeIntervalSince1970]];
NSLog(@"timeSp : %@", timeSp);
- 時(shí)間戳轉(zhuǎn)換成時(shí)間類型(NSDate) 這個(gè)很久寫的斑响,有點(diǎn)亂菱属,自己看吧
時(shí)間戳轉(zhuǎn)換成日期
NSDate *currentTime = [NSDate dateWithTimeIntervalSince1970:[timeSp intValue]];
NSLog(@"currentTime %@",currentTime);
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
NSString * locationString=[dateformatter stringFromDate:currentTime];
NSLog(@"LocationString %@",locationString);
// -------------------------
NSDate *currentTime2 = [NSDate date];
NSLog(@"currentTime2 %@",currentTime2);
NSString *timeSp2 = [NSString stringWithFormat:@"%ld",(long)[currentTime2 timeIntervalSince1970]];
NSDate * current = [NSDate dateWithTimeIntervalSince1970:[timeSp2 intValue]];
NSDateFormatter *dateformatter2=[[NSDateFormatter alloc] init];
[dateformatter2 setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
NSString * locationString2 =[dateformatter2 stringFromDate:current];
NSLog(@"LocationString2 %@",locationString2);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者