Keine Beschreibung

NSNumber+TimeStamp.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // NSNumber+TimeStamp.m
  3. // jinxin
  4. //
  5. // Created by Gavin on 15/1/30.
  6. // Copyright (c) 2015年 jinxin. All rights reserved.
  7. //
  8. #import "NSNumber+TimeStamp.h"
  9. @implementation NSNumber (TimeStamp)
  10. +(NSString *)newTimeforNumber:(NSNumber *)num
  11. {
  12. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  13. NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
  14. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  15. NSDateComponents *componentsWeek = [calendar components:NSWeekdayCalendarUnit fromDate:date];
  16. NSString *weekStr = [weekday objectAtIndex:componentsWeek.weekday];
  17. if ([num isEqualToNumber:@0]||num==nil)
  18. {
  19. return @"";
  20. }
  21. NSCalendar *cal = [NSCalendar currentCalendar];
  22. NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
  23. [components setHour:0];
  24. [components setMinute:0];
  25. [components setSecond:0];
  26. NSDate *today = [cal dateFromComponents:components];
  27. [components setHour:-24];
  28. [components setMinute:0];
  29. [components setSecond:0];
  30. NSDate *yesterday = [cal dateFromComponents:components];
  31. [components setHour:-24*7];
  32. [components setMinute:0];
  33. [components setSecond:0];
  34. NSDate *beforYestoday = [cal dateFromComponents:components];
  35. long long timeStapm1 = [today timeIntervalSince1970]*1000;
  36. long long timeStapm2 = [yesterday timeIntervalSince1970]*1000;
  37. long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000;
  38. // 2.格式化日期
  39. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  40. if (num.longLongValue > timeStapm1)
  41. {
  42. formatter.dateFormat = @"aKK:mm";
  43. }
  44. else if (num.longLongValue > timeStapm2)
  45. {
  46. formatter.dateFormat = @"昨天 aKK:mm";
  47. }
  48. else if (num.longLongValue > timeStapm3)
  49. {
  50. formatter.dateFormat = [NSString stringWithFormat:@"%@ aKK:mm",weekStr];
  51. }
  52. else
  53. {
  54. formatter.dateFormat = @"yy-MM-dd aKK:mm";
  55. }
  56. NSString *time = [formatter stringFromDate:date];
  57. NSRange rang = [time rangeOfString:@"下午00"];
  58. if (rang.length) {
  59. time = [time stringByReplacingOccurrencesOfString:@"下午00" withString:@"中午12"];
  60. }
  61. return time;
  62. }
  63. + (NSString *)timeFromNumer:(NSNumber *)num
  64. {
  65. if ([num isEqualToNumber:@0]||num==nil)
  66. {
  67. return @"";
  68. }
  69. NSCalendar *cal = [NSCalendar currentCalendar];
  70. NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
  71. [components setHour:0];
  72. [components setMinute:0];
  73. [components setSecond:0];
  74. NSDate *today = [cal dateFromComponents:components];
  75. [components setHour:-24];
  76. [components setMinute:0];
  77. [components setSecond:0];
  78. NSDate *yesterday = [cal dateFromComponents:components];
  79. [components setHour:-48];
  80. [components setMinute:0];
  81. [components setSecond:0];
  82. NSDate *beforYestoday = [cal dateFromComponents:components];
  83. long long timeStapm1 = [today timeIntervalSince1970]*1000;
  84. long long timeStapm2 = [yesterday timeIntervalSince1970]*1000;
  85. long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000;
  86. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  87. // 2.格式化日期
  88. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  89. if (num.longLongValue > timeStapm1)
  90. {
  91. formatter.dateFormat = @"HH:mm";
  92. }
  93. else if (num.longLongValue > timeStapm2)
  94. {
  95. formatter.dateFormat = @"昨天 HH:mm";
  96. }
  97. else if (num.longLongValue > timeStapm3)
  98. {
  99. formatter.dateFormat = @"前天 HH:mm";
  100. }
  101. else
  102. {
  103. formatter.dateFormat = @"MM-dd HH:mm";
  104. }
  105. NSString *time = [formatter stringFromDate:date];
  106. return time;
  107. }
  108. + (NSString *)scheduleManagementDetailWith:(NSNumber *)num
  109. {
  110. NSString *dateString = [self strTimeFromNumber:num andDateFormat:@"yyyy/MM/dd"];
  111. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  112. NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
  113. NSCalendar *calendar = [NSCalendar currentCalendar];
  114. NSDateComponents *componentsWeek = [calendar components:NSCalendarUnitWeekday fromDate:date];
  115. NSString *weekStr = [weekday objectAtIndex:componentsWeek.weekday];
  116. if ([num isEqualToNumber:@0]||num==nil)
  117. {
  118. return @"";
  119. }
  120. NSCalendar *cal = [NSCalendar currentCalendar];
  121. NSDateComponents *components = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitWeekOfMonth fromDate:[[NSDate alloc] init]];
  122. [components setHour:0];
  123. [components setMinute:0];
  124. [components setSecond:0];
  125. NSDate *today = [cal dateFromComponents:components];
  126. [components setHour:-24];
  127. [components setMinute:0];
  128. [components setSecond:0];
  129. NSDate *yesterday = [cal dateFromComponents:components];
  130. [components setHour:24];
  131. [components setMinute:0];
  132. [components setSecond:0];
  133. NSDate *tomorrow = [cal dateFromComponents:components];
  134. long long timeStapm1 = [today timeIntervalSince1970]*1000;
  135. long long timeStapm2 = [yesterday timeIntervalSince1970]*1000;
  136. long long timeStapm3 = [tomorrow timeIntervalSince1970]*1000;
  137. NSString *todayString = [self strTimeFromNumber:@(timeStapm1) andDateFormat:@"yyyy/MM/dd"];
  138. NSString *yesterdayString = [self strTimeFromNumber:@(timeStapm2) andDateFormat:@"yyyy/MM/dd"];
  139. NSString *tomorrowString = [self strTimeFromNumber:@(timeStapm3) andDateFormat:@"yyyy/MM/dd"];
  140. if ([dateString isEqualToString:todayString])
  141. {
  142. return @"今天";
  143. }
  144. else if ([dateString isEqualToString:yesterdayString] )
  145. {
  146. return @"昨天";
  147. }else if ([dateString isEqualToString:tomorrowString])
  148. {
  149. return @"明天";
  150. }
  151. else
  152. {
  153. return weekStr;
  154. }
  155. }
  156. + (NSString *)shortTimeFromNumer:(NSNumber *)num
  157. {
  158. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  159. NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
  160. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  161. NSDateComponents *componentsWeek = [calendar components:NSWeekdayCalendarUnit fromDate:date];
  162. NSString *weekStr = [weekday objectAtIndex:componentsWeek.weekday];
  163. if ([num isEqualToNumber:@0]||num==nil)
  164. {
  165. return @"";
  166. }
  167. NSCalendar *cal = [NSCalendar currentCalendar];
  168. NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
  169. [components setHour:0];
  170. [components setMinute:0];
  171. [components setSecond:0];
  172. NSDate *today = [cal dateFromComponents:components];
  173. [components setHour:-24];
  174. [components setMinute:0];
  175. [components setSecond:0];
  176. NSDate *yesterday = [cal dateFromComponents:components];
  177. [components setHour:-24 * 7];
  178. [components setMinute:0];
  179. [components setSecond:0];
  180. NSDate *beforYestoday = [cal dateFromComponents:components];
  181. long long timeStapm1 = [today timeIntervalSince1970]*1000;
  182. long long timeStapm2 = [yesterday timeIntervalSince1970]*1000;
  183. long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000;
  184. // 2.格式化日期
  185. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  186. if (num.longLongValue > timeStapm1)
  187. {
  188. formatter.dateFormat = @"aKK:mm";
  189. }
  190. else if (num.longLongValue > timeStapm2)
  191. {
  192. return @"昨天";
  193. // formatter.dateFormat = @"昨天 HH:mm";
  194. }
  195. else if (num.longLongValue > timeStapm3)
  196. {
  197. return weekStr;
  198. }
  199. else
  200. {
  201. formatter.dateFormat = @"yy/MM/dd";
  202. }
  203. NSString *time = [formatter stringFromDate:date];
  204. NSRange rang = [time rangeOfString:@"下午00"];
  205. if (rang.length) {
  206. time = [time stringByReplacingOccurrencesOfString:@"下午00" withString:@"中午12"];
  207. }
  208. return time;
  209. }
  210. + (NSString *)hourAndMinuteWith:(NSNumber *)num
  211. {
  212. if ([num isEqualToNumber:@0]||num==nil)
  213. {
  214. return @"";
  215. }
  216. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  217. // 2.格式化日期
  218. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  219. formatter.dateFormat = @"HH:mm";
  220. NSString *time = [formatter stringFromDate:date];
  221. return time;
  222. }
  223. + (NSString *)stdTimeFromNumer:(NSNumber *)num
  224. {
  225. if ([num isEqualToNumber:@0]||num==nil)
  226. {
  227. return @"";
  228. }
  229. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  230. // 2.格式化日期
  231. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  232. formatter.dateFormat = @"MM-dd HH:mm:ss";
  233. NSString *time = [formatter stringFromDate:date];
  234. return time;
  235. }
  236. +(NSString *)strYMDTimeFromNumber:(NSNumber *)num
  237. {
  238. if ([num isEqualToNumber:@0]||num==nil)
  239. {
  240. return @"";
  241. }
  242. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  243. // 2.格式化日期
  244. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  245. formatter.dateFormat = @"yyyy年MM月dd日";
  246. NSString *time = [formatter stringFromDate:date];
  247. return time;
  248. }
  249. + (NSNumber *)currentTimeStamp
  250. {
  251. NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  252. NSTimeInterval a = [dat timeIntervalSince1970]*1000;
  253. long long num = llround(a);
  254. NSNumber *time = [NSNumber numberWithLongLong:num];
  255. return time;
  256. }
  257. +(NSString *)strTimeFromNumber:(NSNumber *)num
  258. {
  259. if ([num isEqualToNumber:@0]||num==nil)
  260. {
  261. return @"";
  262. }
  263. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  264. // 2.格式化日期
  265. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  266. formatter.dateFormat = @"yyyy年MM月dd日 HH:mm";
  267. NSString *time = [formatter stringFromDate:date];
  268. return time;
  269. }
  270. +(NSString *)strTimeFromNumber:(NSNumber *)num andDateFormat:(NSString *)dateFormat{
  271. if ([num isEqualToNumber:@0]||num==nil)
  272. {
  273. return @"";
  274. }
  275. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  276. // 2.格式化日期
  277. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  278. formatter.dateFormat = dateFormat;
  279. NSString *time = [formatter stringFromDate:date];
  280. return time;
  281. }
  282. + (NSNumber *)changeNumberWithDate:(NSDate *)date
  283. {
  284. NSTimeInterval a = [date timeIntervalSince1970]*1000;
  285. long long num = llround(a);
  286. return [NSNumber numberWithLongLong:num];
  287. }
  288. + (NSString *)YMDFromNumer:(NSNumber *)num
  289. {
  290. if ([num isEqualToNumber:@0]||num==nil)
  291. {
  292. return @"";
  293. }
  294. NSCalendar *cal = [NSCalendar currentCalendar];
  295. NSDateComponents *components = [cal components:( NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
  296. [components setHour:0];
  297. [components setMinute:0];
  298. [components setSecond:0];
  299. NSDate *today = [cal dateFromComponents:components];
  300. [components setHour:-24];
  301. [components setMinute:0];
  302. [components setSecond:0];
  303. NSDate *yesterday = [cal dateFromComponents:components];
  304. [components setHour:-48];
  305. [components setMinute:0];
  306. [components setSecond:0];
  307. NSDate *beforYestoday = [cal dateFromComponents:components];
  308. long long timeStapm1 = [today timeIntervalSince1970]*1000;
  309. long long timeStapm2 = [yesterday timeIntervalSince1970]*1000;
  310. long long timeStapm3 = [beforYestoday timeIntervalSince1970]*1000;
  311. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[num doubleValue]/1000];
  312. // 2.格式化日期
  313. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  314. if (num.longLongValue >= timeStapm1)
  315. {
  316. formatter.dateFormat = @"今天 ";
  317. }
  318. else if (num.longLongValue >= timeStapm2)
  319. {
  320. formatter.dateFormat = @"昨天 ";
  321. }
  322. else if (num.longLongValue >= timeStapm3)
  323. {
  324. formatter.dateFormat = @"前天 ";
  325. }
  326. else
  327. {
  328. formatter.dateFormat = @"MM-dd ";
  329. }
  330. NSString *time = [formatter stringFromDate:date];
  331. return time;
  332. }
  333. +(NSDate *)changeDateStringWith:(NSString *)timeString
  334. {
  335. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  336. [formatter setDateStyle:NSDateFormatterMediumStyle];
  337. [formatter setTimeStyle:NSDateFormatterShortStyle];
  338. [formatter setDateFormat:@"YYYY-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  339. //设置时区,这个对于时间的处理有时很重要
  340. //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
  341. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
  342. //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
  343. // NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
  344. NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
  345. [formatter setTimeZone:timeZone];
  346. return [formatter dateFromString:timeString];
  347. }
  348. +(NSDate *)changeDateStringWith:(NSString *)timeString andFormatter:(NSString *)formatterString
  349. {
  350. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  351. [formatter setDateStyle:NSDateFormatterMediumStyle];
  352. [formatter setTimeStyle:NSDateFormatterShortStyle];
  353. [formatter setDateFormat:formatterString]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  354. //设置时区,这个对于时间的处理有时很重要
  355. //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
  356. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
  357. //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
  358. NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
  359. [formatter setTimeZone:timeZone];
  360. return [formatter dateFromString:timeString];
  361. }
  362. +(NSDate *)changeDateHmsStringWith:(NSString *)timeString{
  363. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  364. [formatter setDateStyle:NSDateFormatterMediumStyle];
  365. [formatter setTimeStyle:NSDateFormatterShortStyle];
  366. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  367. //设置时区,这个对于时间的处理有时很重要
  368. //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
  369. //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
  370. //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
  371. // NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
  372. NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
  373. [formatter setTimeZone:timeZone];
  374. return [formatter dateFromString:timeString];
  375. }
  376. + (NSNumber *)timeStampFromString:(NSString *)dateString
  377. {
  378. NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
  379. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  380. NSDate *date = [dateFormatter dateFromString:dateString];
  381. NSTimeInterval time = [date timeIntervalSince1970]*1000;
  382. return [NSNumber numberWithLongLong:time];
  383. }
  384. @end