时间戳
皆为整理所得 如有错误请批评指正 共同学习 谢过
Date不是Data Date不是Data Date不是Data 重要的事说三遍
网络请求的时间戳一般分为两种
* 年月日字符串
* 秒数 [计算当年时间距离1970年1月1日的秒数]
一般我们常遇到的问题就是13位时间戳转换、格式化时间以及截取时间
如果碰到13位时间戳要除以1000哦 [字符串13 转时间]
NSString * timeStampString = @"1448697828";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / 1000];
NSLog(@"%.f", ([date timeIntervalSince1970] * 1000.0));
只要20150206的话[时间戳转时间]
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]);
另:相对于NSDateFormatter 还有一种方法 结果: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);
距离某个时间戳有多长时间
输出:From Now to 2016-01-19 07:38:45 +0000, diff: Years: 0 Months: 1, Days; 21, Hours: 22, Mins:39, sec:47
通过该方法可以判断消息发送时间
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
- 格式化时间去掉时区并转换为字符串
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小时制 hh12小时制
- 当前日期转换为时间戳
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); // timeSp:1448697828
此处请参考 enter link description here
输出格式通setDateStyle和setTimeStyle设置,分别定义的日期和时间的格式可选一下的系统给出的方法
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;
网友评论