iOS-NSDate时间、时间戳操作

电玩女神 2023-10-16 23:53 104阅读 0赞
  1. #pragma mark -获取字符串形式的时间戳
  2. -(NSString *)getTimeStampString
  3. {
  4. //获取时间和时间戳
  5. NSDate* timeStamp = [NSDatedateWithTimeIntervalSinceNow:0];
  6. NSTimeInterval temp=[timeStamptimeIntervalSince1970]*1000;
  7. return [NSStringstringWithFormat:@"%.0f", temp];
  8. }
  9. #pragma mark - 时间戳转为NSString
  10. /**
  11. * @author zm
  12. *
  13. * @brief 时间戳转为NSString
  14. *
  15. * @param timeStamp 字符串格式的时间戳
  16. * @param formatString format格式(默认yyyy-MM-dd HH:mm:ss.SSS)
  17. *
  18. * @return 返回字符串
  19. */
  20. -(NSString *)event_stringFromTimeStamp:(NSString *)timeStamp formatString:(NSString *)formatString
  21. {
  22. NSDate *updatetimestamp = [selff_getDateByString:timeStamp];
  23. NSString *dateFormat =@"yyyy-MM-dd HH:mm:ss.SSS";
  24. if (formatString) {
  25. dateFormat = formatString;
  26. }
  27. NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
  28. [formatter setDateStyle:NSDateFormatterMediumStyle];
  29. [formatter setTimeStyle:NSDateFormatterShortStyle];
  30. [formatter setDateFormat:dateFormat];
  31. NSString *dateString = [formatterstringFromDate:updatetimestamp];
  32. return dateString;
  33. }
  34. #pragma mark -获取字符串形式的当前时间(默认yyyy-MM-dd HH:mm:ss)
  35. +(NSString *)getDateTimeString:(NSDateFormatter *)dateFormatter
  36. {
  37. if (dateFormatter ==nil) {
  38. dateFormatter = [[NSDateFormatteralloc]init];
  39. [dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
  40. }
  41. NSDate *dateNow = [NSDatedate];
  42. return [dateFormatterstringFromDate:dateNow];
  43. }
  44. #pragma mark -将字符串形式的时间转换为时间戳
  45. +(UInt64)getTimeStampByDateString:(NSString *)dateString
  46. {
  47. NSDateFormatter *XNNDateFormatter = [[NSDateFormatteralloc]init];
  48. [XNNDateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
  49. NSDate *XNNDate= [XNNDateFormatterdateFromString:dateString];
  50. if(XNNDate){
  51. return [XNNDatetimeIntervalSince1970]*1000;
  52. }else{
  53. return -99999;
  54. }
  55. }
  56. #pragma mark -字符串转为日期
  57. - (NSDate *)stringToDate:(NSString *)strdate
  58. {
  59. NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
  60. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  61. NSDate *retdate = [dateFormatter dateFromString:strdate];
  62. [dateFormatter release];
  63. return retdate;
  64. }
  65. #pragma mark -日期转为字符串
  66. - (NSString *)dateToString:(NSDate *)date
  67. {
  68. NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
  69. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  70. NSString *strDate = [dateFormatter stringFromDate:date];
  71. [dateFormatter release];
  72. return strDate;
  73. }
  74. #pragma mark -将UTC日期字符串转为本地时间字符串
  75. //输入的UTC日期格式2013-08-03T04:53:51+0000
  76. -(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate
  77. {
  78. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  79. //输入格式
  80. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
  81. NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
  82. [dateFormatter setTimeZone:localTimeZone];
  83. NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
  84. //输出格式
  85. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  86. NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
  87. [dateFormatter release];
  88. return dateString;
  89. }
  90. #pragma mark -将本地日期字符串转为UTC日期字符串
  91. //本地日期格式:2013-08-03 12:53:51
  92. //可自行指定输入输出格式
  93. -(NSString *)getUTCFormateLocalDate:(NSString *)localDate
  94. {
  95. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  96. //输入格式
  97. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  98. NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
  99. NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
  100. [dateFormatter setTimeZone:timeZone];
  101. //输出格式
  102. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
  103. NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
  104. [dateFormatter release];
  105. return dateString;
  106. }
  107. - (NSString *)getUTCFormatDate:(NSDate *)localDate
  108. {
  109. NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
  110. NSTimeZone *timeZone = [NSTimeZonetimeZoneWithName:@"UTC"];
  111. [dateFormatter setTimeZone:timeZone];
  112. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
  113. NSString *dateString = [dateFormatter stringFromDate:localDate];
  114. [dateFormatter release];
  115. return dateString;
  116. }
  117. - (NSDate *)getLocalFromUTC:(NSString *)utc
  118. {
  119. NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
  120. NSTimeZone *timeZone = [NSTimeZonelocalTimeZone];
  121. [dateFormatter setTimeZone:timeZone];
  122. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
  123. NSDate *ldate = [dateFormatter dateFromString:utc];
  124. [dateFormatter release];
  125. return ldate;
  126. }
  127. #pragma mark -世界标准时间UTC /GMT 转为当前系统时区对应的时间
  128. - (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
  129. {
  130. //设置源日期时区
  131. NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
  132. //设置转换后的目标日期时区
  133. NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
  134. //得到源日期与世界标准时间的偏移量
  135. NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
  136. //目标日期与本地时区的偏移量
  137. NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
  138. //得到时间偏移量的差值
  139. NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
  140. //转为现在时间
  141. NSDate* destinationDateNow = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate] autorelease];
  142. return destinationDateNow;
  143. }
  144. 如:
  145. //2013-08-03T12:53:51+0800 UTC时间格式下的北京时间,可以看到北京时间= UTC + 8小时。
  146. NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];
  147. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
  148. NSDate *localDate = [dateFormatter dateFromString:@"2013-08-03T04:56:52+0000"]; 0000 表示的是当前时间是个世界时间。
  149. [dateFormatter release];
  150. NSLog(@"now Time = %@",[selfgetNowDateFromatAnDate:localDate]);
  151. 结果:
  152. 2013-08-03 12:57:33.391 xxxx[2547:c07] now Time = 2013-08-03 12:56:52 +0000
  153. 以上注意一点,在转出来后带的时间是原参数anydate的时区,因此切不可再用NSDateFormatter 转换。否则会多增加一个时区的时间值。应该使用如下来提取字符串
  154. NSString *str = [NSStringstringWithFormat:@"%@",[selfgetNowDateFromatAnDate:localDate]];
  155. NSLog(@"str = %@",str);
  156. NSDate对象存放的日期始终是UTC的标准时间,可以根据这个时间进行其它时间的转换。因此上面算出来的时间中时区为 +0000,如果此时再转为字符串
  157. #pragma mark -比较时间间隔(天数)
  158. +(NSInteger)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
  159. {
  160. NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];
  161. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  162. NSTimeInterval time =[oneDaytimeIntervalSinceDate:anotherDay];
  163. NSInteger days=((NSInteger)time)/(3600*24);
  164. return days;
  165. }
  166. #pragma mark -比较时间间隔(ms
  167. +(NSInteger)compareOneTime:(NSDate *)oneTime withAnotherTime:(NSDate *)anotherTime
  168. {
  169. NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];
  170. [dateFormatter setDateFormat:@"HH:mm:ss"];
  171. NSTimeInterval time =[oneTimetimeIntervalSinceDate:anotherTime];
  172. //1s = 1000ms
  173. NSInteger timeT=((NSInteger)time) *1000;
  174. return timeT;
  175. }
  176. #pragma mark-获取当前时间的年、月、日、时、分、秒
  177. //获取当前时间
  178. NSDate *now = [NSDate date];
  179. NSLog(@"now date is: %@", now);
  180. NSCalendar *calendar = [NSCalendar currentCalendar];
  181. NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
  182. NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
  183. int year = [dateComponent year];
  184. int month = [dateComponent month];
  185. int day = [dateComponent day];
  186. int hour = [dateComponent hour];
  187. int minute = [dateComponent minute];
  188. int second = [dateComponent second];
  189. NSLog(@"year is: %d", year);
  190. NSLog(@"month is: %d", month);
  191. NSLog(@"day is: %d", day);
  192. NSLog(@"hour is: %d", hour);
  193. NSLog(@"minute is: %d", minute);
  194. NSLog(@"second is: %d", second);







   
  1. NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

发表评论

表情:
评论列表 (有 0 条评论,104人围观)

还没有评论,来说两句吧...

相关阅读