時(shí)間戳
皆為整理所得 如有錯(cuò)誤請(qǐng)批評(píng)指正 共同學(xué)習(xí) 謝過
Date不是Data Date不是Data Date不是Data 重要的事說三遍
網(wǎng)絡(luò)請(qǐng)求的時(shí)間戳一般分為兩種
* 年月日字符串
* 秒數(shù) [計(jì)算當(dāng)年時(shí)間距離1970年1月1日的秒數(shù)]
一般我們常遇到的問題就是13位時(shí)間戳轉(zhuǎn)換流译、格式化時(shí)間以及截取時(shí)間
如果碰到13位時(shí)間戳要除以1000哦 [字符串13 轉(zhuǎn)時(shí)間]
NSString * timeStampString = @"1448697828";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / 1000];
NSLog(@"%.f", ([date timeIntervalSince1970] * 1000.0));
只要20150206的話[時(shí)間戳轉(zhuǎn)時(shí)間]
NSString * timeStampString = @"1423189125873";
NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
[objDateformat setDateFormat:@"yyyyMMdd"];
NSLog(@"%@", [objDateformat stringFromDate: date]);
另:相對(duì)于NSDateFormatter 還有一種方法 結(jié)果:2015-11-28 00:00:00 +0000
- (NSDate *)extractDate:(NSDate *)date {
//get seconds since 1970
NSTimeInterval interval = [date timeIntervalSince1970];
int oneDay = 24 * 60 * 60;
//calculate integer type of days
NSInteger allDays = interval / oneDay;
return [NSDate dateWithTimeIntervalSince1970:allDays * oneDay];
}
20151128是周幾
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:28];
[comps setMonth:11];
[comps setYear:2015];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
NSDateComponents *weekdayComponents =
[gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
NSLog(@"%d",weekday);
距離某個(gè)時(shí)間戳有多長時(shí)間
輸出:From Now to 2016-01-19 07:38:45 +0000, diff: Years: 0 Months: 1, Days; 21, Hours: 22, Mins:39, sec:47
通過該方法可以判斷消息發(fā)送時(shí)間
NSDate* toDate = [ [ NSDate alloc] initWithTimeIntervalSince1970:1453189125];
NSDate* startDate = [ [ NSDate alloc] init ];
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUInteger unitFlags =
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:startDate toDate:toDate options:0];
NSInteger diffHour = [cps hour];
NSInteger diffMin = [cps minute];
NSInteger diffSec = [cps second];
NSInteger diffDay = [cps day];
NSInteger diffMon = [cps month];
NSInteger diffYear = [cps year];
NSLog( @" From Now to %@, diff: Years: %ld Months: %ld, Days; %ld, Hours: %ld, Mins:%ld, sec:%ld",[toDate description], diffYear, (long)diffMon, (long)diffDay, (long)diffHour, (long)diffMin,(long)diffSec );
控件上只能放字符串 無法將NSDate顯示在控件上 所以 用 NSDateFormatter
- 格式化時(shí)間去掉時(shí)區(qū)并轉(zhuǎn)換為字符串
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [NSDate date];
NSLog(@"%@",date); // 2015-11-28 07:17:23 +0000
NSString *str = [formatter stringFromDate:date];
NSLog(@"%@",str); // 2015-11-28 15:17:23
** HH24小時(shí)制 hh12小時(shí)制
- 當(dāng)前日期轉(zhuǎn)換為時(shí)間戳
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); // timeSp:1448697828
此處請(qǐng)參考 enter link description here
輸出格式通setDateStyle和setTimeStyle設(shè)置培廓,分別定義的日期和時(shí)間的格式可選一下的系統(tǒng)給出的方法
typedef enum {
NSDateFormatterNoStyle = kCFDateFormatterNoStyle,
NSDateFormatterShortStyle = kCFDateFormatterShortStyle,//“11/23/37” or “3:30pm”
NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,//\"Nov 23, 1937\"
NSDateFormatterLongStyle = kCFDateFormatterLongStyle,//\"November 23, 1937” or “3:30:32pm\"
NSDateFormatterFullStyle = kCFDateFormatterFullStyle//“Tuesday, April 12, 1952 AD” or “3:30:42pm PST”
} NSDateFormatterStyle;