您的位置:首页 > 移动开发 > IOS开发

[置顶] iOS关于NSDate时间、时间戳操作

2016-05-27 16:51 633 查看
#pragma mark -获取字符串形式的时间戳
-(NSString *)getTimeStampString
{
//获取时间和时间戳
NSDate* timeStamp = [NSDatedateWithTimeIntervalSinceNow:0];
NSTimeInterval temp=[timeStamptimeIntervalSince1970]*1000;
return [NSStringstringWithFormat:@"%.0f", temp];
}

#pragma mark - 时间戳转为NSString
/**
*  @author zm
*
*  @brief  时间戳转为NSString
*
*  @param timeStamp   字符串格式的时间戳
*  @param formatString format格式(默认yyyy-MM-dd HH:mm:ss.SSS)
*
*  @return 返回字符串
*/
-(NSString *)event_stringFromTimeStamp:(NSString *)timeStamp formatString:(NSString *)formatString
{
NSDate *updatetimestamp = [selff_getDateByString:timeStamp];
NSString *dateFormat =@"yyyy-MM-dd HH:mm:ss.SSS";
if (formatString) {
dateFormat = formatString;
}
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:dateFormat];
NSString *dateString = [formatterstringFromDate:updatetimestamp];
return dateString;
}

#pragma mark -获取字符串形式的当前时间(默认yyyy-MM-dd HH:mm:ss)
+(NSString *)getDateTimeString:(NSDateFormatter *)dateFormatter
{
if (dateFormatter ==nil) {
dateFormatter = [[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
}
NSDate *dateNow = [NSDatedate];
return [dateFormatterstringFromDate:dateNow];
}

#pragma mark -将字符串形式的时间转换为时间戳
+(UInt64)getTimeStampByDateString:(NSString *)dateString
{
NSDateFormatter *XNNDateFormatter = [[NSDateFormatteralloc]init];
[XNNDateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *XNNDate= [XNNDateFormatterdateFromString:dateString];
if(XNNDate){
return [XNNDatetimeIntervalSince1970]*1000;
}else{
return -99999;
}
}

#pragma mark -字符串转为日期
- (NSDate *)stringToDate:(NSString *)strdate
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *retdate = [dateFormatter dateFromString:strdate];
[dateFormatter release];
return retdate;
}

#pragma mark -日期转为字符串
- (NSString *)dateToString:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
return strDate;
}

#pragma mark -将UTC日期字符串转为本地时间字符串
//输入的UTC日期格式2013-08-03T04:53:51+0000
-(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:localTimeZone];

NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
//输出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}

#pragma mark -将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
-(NSString *)getUTCFormateLocalDate:(NSString *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//输入格式
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
//输出格式
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}

- (NSString *)getUTCFormatDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
NSTimeZone *timeZone = [NSTimeZonetimeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}

- (NSDate *)getLocalFromUTC:(NSString *)utc
{
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
NSTimeZone *timeZone = [NSTimeZonelocalTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *ldate = [dateFormatter dateFromString:utc];
[dateFormatter release];
return ldate;
}

#pragma mark -世界标准时间UTC /GMT 转为当前系统时区对应的时间
- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
{
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] autorelease];
return destinationDateNow;
}
如:
//2013-08-03T12:53:51+0800     UTC时间格式下的北京时间,可以看到北京时间= UTC + 8小时。
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *localDate = [dateFormatter dateFromString:@"2013-08-03T04:56:52+0000"];      +0000 表示的是当前时间是个世界时间。
[dateFormatter release];
NSLog(@"now Time = %@",[selfgetNowDateFromatAnDate:localDate]);
结果:
2013-08-03 12:57:33.391 xxxx[2547:c07] now Time = 2013-08-03 12:56:52 +0000
以上注意一点,在转出来后带的时间是原参数anydate的时区,因此切不可再用NSDateFormatter 转换。否则会多增加一个时区的时间值。应该使用如下来提取字符串
NSString *str = [NSStringstringWithFormat:@"%@",[selfgetNowDateFromatAnDate:localDate]];
NSLog(@"str = %@",str);
注NSDate对象存放的日期始终是UTC的标准时间,可以根据这个时间进行其它时间的转换。因此上面算出来的时间中时区为 +0000,如果此时再转为字符串
#pragma mark -比较时间间隔(天数)
+(NSInteger)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSTimeInterval time =[oneDaytimeIntervalSinceDate:anotherDay];
NSInteger days=((NSInteger)time)/(3600*24);
return days;
}

#pragma mark -比较时间间隔(ms)
+(NSInteger)compareOneTime:(NSDate *)oneTime withAnotherTime:(NSDate *)anotherTime
{
NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSTimeInterval time =[oneTimetimeIntervalSinceDate:anotherTime];
//1s = 1000ms
NSInteger timeT=((NSInteger)time) *1000;
return timeT;
}

#pragma mark-获取当前时间的年、月、日、时、分、秒
//获取当前时间
NSDate *now = [NSDate date];
NSLog(@"now date is: %@", now);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
int year = [dateComponent year];
int month = [dateComponent month];
int day = [dateComponent day];
int hour = [dateComponent hour];
int minute = [dateComponent minute];
int second = [dateComponent second];
NSLog(@"year is: %d", year);
NSLog(@"month is: %d", month);
NSLog(@"day is: %d", day);
NSLog(@"hour is: %d", hour);
NSLog(@"minute is: %d", minute);
NSLog(@"second is: %d", second);


  
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: