本人搜索了很多关于iOS中时间与时间戳的相互转化的资料,下面我来记录一下,有需要了解iOS中时间与时间戳的相互转化的朋友可参考。希望此文章对各位有所帮助。
//获取当前系统时间的时间戳 # pragma马克-获取当前时间的时间戳 + (NSInteger) getNowTimestamp { 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日期];//现在时间 NSLog(@“设备当前的时间:% @”,[格式化程序stringFromDate: datenow]);//时间转时间戳的方法: NSInteger时代不同=[[NSNumber numberWithDouble:(datenow timeIntervalSince1970]] integerValue); NSLog(@“设备当前的时间戳:% ld”,(长)时代不同);//时间戳的值 返回时代不同; }//将某个时间转化成时间戳 # pragma马克-将某个时间转化成时间戳 + (NSInteger) timeSwitchTimestamp:(NSString *) formatTime andFormatter: {(NSString *)格式 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 *日期=[格式化程序dateFromString formatTime):;//- - - - - - - - - - - -将字符串按格式化程序转成nsdate//时间转时间戳的方法: NSInteger时代不同=[[NSNumber numberWithDouble:[日期timeIntervalSince1970]] integerValue); NSLog(@“将某个时间转化成时间戳,,,,,,,时代不同:% ld”,(长)时代不同);//时间戳的值 返回时代不同; }//将某个时间戳转化成时间 # pragma马克-将某个时间戳转化成时间 + (NSString *) timestampSwitchTime:(NSInteger)时间戳andFormatter: {(NSString *)格式 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 * confromTimesp=[NSDate dateWithTimeIntervalSince1970时间戳):; NSLog (@“1296035591=% @”, confromTimesp); NSString * confromTimespStr=[格式化程序stringFromDate confromTimesp):;//NSLog (@”,,,,,,, confromTimespStr=: % @”, confromTimespStr); 返回confromTimespStr; } >之前以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
iOS中时间与时间戳的相互转化实例代码