当前时间 :[NSDate date] 格式 2014-05-21 04:47:37 +0000 (与当前北京时间差8个小时【北京时间:2014-05-21 12:47:37 +0000】)
当前时间戳:[[NSDate date] timeIntervalSince1970] (结果为 NSTimeInterval 格式的长串)
当前时间距离自定义时间间隔:[[NSDate date] timeIntervalSinceDate:某个自定义时间]
/**
格式化时间
timeSeconds 为0时表示当前时间,可以传入你定义的时间戳
timeFormatStr为空返回当当时间戳,不为空返回你写的时间格式(yyyy-MM-dd HH:ii:ss)
setTimeZome ([NSTimeZone systemTimeZone]获得当前时区字符串)
*/
-(NSString *)setTimeInt:(NSTimeInterval)timeSeconds setTimeFormat:(NSString *)timeFormatStr setTimeZome:(NSString *)timeZoneStr
{
NSString *date_string;
NSDate *time_str;
if( timeSeconds>0){
time_str =[NSDate dateWithTimeIntervalSince1970:timeSeconds];
}else{
time_str=[[NSDate alloc] init];
}
if( timeFormatStr==nil){
date_string =[NSString stringWithFormat:@"%d",(long)[time_str timeIntervalSince1970]];
}else{
NSDateFormatter *date_format_str =[[[NSDateFormatter alloc] init] autorelease];
[date_format_str setDateFormat:timeFormatStr];
if( timeZoneStr!=nil){
[date_format_str setTimeZone:[NSTimeZone timeZoneWithName:timeZoneStr]];
}
date_string =[date_format_str stringFromDate:time_str];
}
return date_string;
}
/**
*用法
*/
-(void)viewWillAppear:(BOOL)animated
{
NSString *a =[self setTimeInt:1317914496 setTimeFormat:@"yy.MM.dd HH:mm:ss" setTimeZome:nil];
NSString *b =[self setTimeInt:0 setTimeFormat:@"yy.MM.dd HH:mm:ss" setTimeZome:nil];
NSString *c =[self setTimeInt:0 setTimeFormat:nil setTimeZome:nil];
NSString *d =[self setTimeInt:0 setTimeFormat:@"yy.MM.dd HH:mm:ss" setTimeZome:@"GMT"];
NSLog(@"%@,,,%@,,,%@,,,%@",a,b,c,d);
}