#pragma mark -获取字符串形式的时间戳
-(NSString*)getTimeStampString
{
//获取时间和时间戳
NSDate* timeStamp = [NSDatedateWithTimeIntervalSinceNow:0];
NSTimeIntervaltemp=[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];
returndateString;
}
#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];
returndateString;
}
#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];
returndateString;
}
- (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;
}
- (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];
returndestinationDateNow;
}
如:
//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);