//获取当前的时间 + (NSString *) getCurrentTimes { NSDateFormatter *格式化程序=[[NSDateFormatter alloc] init);//- - - - - - - - - - -设置你想要的格式,hh与hh的区别:分别表示12小时制,24小时制 【格式化程序setDateFormat: @“YYYY-MM-dd HH: mm: ss”);//现在时间,你可以输出来看下是什么格式 NSDate * datenow=(NSDate日期);//- - - - - - - - - - -将nsdate按格式化程序格式转成nsstring NSString * currentTimeString=[格式化程序stringFromDate datenow):; NSLog (@“currentTimeString=% @”currentTimeString); 返回currentTimeString; }
获取当前时间戳有两种方法(以秒为单位)
+ (NSString *) getNowTimeTimestamp { NSDateFormatter *格式化程序=[[NSDateFormatter alloc] init); [格式化程序setDateStyle: NSDateFormatterMediumStyle]; [格式化程序setTimeStyle: NSDateFormatterShortStyle]; 【格式化程序setDateFormat: @“YYYY-MM-dd HH: mm: ss”);//- - - - - - - - - - -设置你想要的格式,hh与hh的区别:分别表示12小时制,24小时制//设置时区,这个对于时间的处理有时很重要 NSTimeZone *时区=[NSTimeZone timeZoneWithName: @“亚洲/上海”); (formatter setTimeZone:时区); NSDate * datenow=[NSDate日期];//现在时间,你可以输出来看下是什么格式 NSString *时代不同=[NSString stringWithFormat: @ % ld》(长)[datenow timeIntervalSince1970]]; 返回时代不同; } + (NSString *) getNowTimeTimestamp2 { NSDate * dat=(NSDate dateWithTimeIntervalSinceNow: 0); NSTimeInterval=(dat timeIntervalSince1970); NSString * timeString=[NSString stringWithFormat: @“% 0。f ");//转为字符型 ; 返回timeString; }//获取当前时间戳(以毫秒为单位) + (NSString *) getNowTimeTimestamp3 { NSDateFormatter *格式化程序=[[NSDateFormatter alloc] init); [格式化程序setDateStyle: NSDateFormatterMediumStyle]; [格式化程序setTimeStyle: NSDateFormatterShortStyle]; 【格式化程序setDateFormat: @“YYYY-MM-dd HH: mm: ss SSS”);//- - - - - - - - - - -设置你想要的格式,hh与hh的区别:分别表示12小时制,24小时制//设置时区,这个对于时间的处理有时很重要 NSTimeZone *时区=[NSTimeZone timeZoneWithName: @“亚洲/上海”); (formatter setTimeZone:时区); NSDate * datenow=[NSDate日期];//现在时间,你可以输出来看下是什么格式 NSString *时代不同=[NSString stringWithFormat: @ % ld》(长)[datenow timeIntervalSince1970] * 1000); 返回时代不同; }
以上所述是小编给大家介绍的iOS获取当前时间和当前时间戳的方法,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!