悟空记账

JZTime.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // JZTime.m
  3. // JIZHANG
  4. //
  5. // Created by kuxuan on 2017/10/24.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "JZTime.h"
  9. @implementation JZTime
  10. +(NSString *)getTime{
  11. NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
  12. long long int date = (long long int)time;
  13. NSString *timeString = [NSString stringWithFormat:@"%lld", date];
  14. return timeString;
  15. }
  16. +(NSString *)getYYMMDDWithDate:(NSDate *)date{
  17. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  18. [formatter setDateFormat:@"yy-MM-dd"];
  19. NSString *dateStr = [formatter stringFromDate:date];
  20. return dateStr;
  21. }
  22. +(NSString *)getYYMMDDWithDate2:(NSDate *)date{
  23. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  24. [formatter setDateFormat:@"yyyy-MM-dd"];
  25. NSString *dateStr = [formatter stringFromDate:date];
  26. return dateStr;
  27. }
  28. +(NSString *)getYYMMDDHHMMWithDate:(NSDate *)date{
  29. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  30. [formatter setDateFormat:@"yyyy年MM月dd日"];
  31. NSString *dateStr = [formatter stringFromDate:date];
  32. return dateStr;
  33. }
  34. +(NSString *)getYYMMDDHHMMWithDate2:(NSDate *)date{
  35. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  36. [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
  37. NSString *dateStr = [formatter stringFromDate:date];
  38. return dateStr;
  39. }
  40. +(NSString *)getYear:(NSDate *)date
  41. {
  42. NSString * dateString = [self getYYMMDDWithDate2:date];
  43. NSArray *dateArray = [dateString componentsSeparatedByString:@"-"];
  44. return dateArray[0];
  45. }
  46. +(NSString *)getMonth:(NSDate *)date
  47. {
  48. NSString * dateString = [self getYYMMDDWithDate2:date];
  49. NSArray *dateArray = [dateString componentsSeparatedByString:@"-"];
  50. NSString *twoString = dateArray[1];
  51. if ([twoString hasPrefix:@"0"]) {
  52. twoString = [twoString substringFromIndex:1];
  53. }
  54. return twoString;
  55. }
  56. +(NSString *)getTimeFromTimeString:(NSString *)timeString
  57. {
  58. NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue/1000];
  59. NSDate *localDate = [NSDate date];
  60. NSInteger interval = localDate.timeIntervalSince1970 - date.timeIntervalSince1970;
  61. interval=interval/60/60/24;
  62. if (interval<1.0) {
  63. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  64. [formatter setDateFormat:@"HH:mm"];
  65. NSString *dateStr = [formatter stringFromDate:date];
  66. return dateStr;
  67. }else if(interval<2.0){
  68. return @"昨天";
  69. }else if(interval<3.0){
  70. return @"前天";
  71. }else{
  72. //日期
  73. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  74. [formatter setDateFormat:@"yyyy-MM-dd"];
  75. NSString *dateStr = [formatter stringFromDate:date];
  76. return dateStr;
  77. }
  78. return @"";
  79. }
  80. +(NSString *)getTimeWithSice1970TimeString:(NSString *)timeString{
  81. NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/1000];
  82. NSDate *localDate = [NSDate date];
  83. NSInteger interval = localDate.timeIntervalSince1970 - date.timeIntervalSince1970;
  84. interval = interval/60;//分钟为单位
  85. if (interval <= 60) {
  86. if (interval <= 30) {
  87. if (interval == 0){
  88. return @"刚刚";
  89. }else{
  90. return [NSString stringWithFormat:@"%ld分钟前",(long)interval];
  91. }
  92. }else if (interval > 30){
  93. return @"半小时前";
  94. }
  95. }else{
  96. interval = interval/60;
  97. if (interval <= 24) {
  98. return [NSString stringWithFormat:@"%ld小时前",(long)interval];
  99. }else{
  100. //今年
  101. NSDate * senddate=[NSDate date];
  102. NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
  103. [dateformatter setDateFormat:@"YYYY"];
  104. NSString * locationString=[dateformatter stringFromDate:senddate];
  105. //日期
  106. NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
  107. [formatter setDateFormat:@"yyyy"];
  108. NSString *dateStr = [formatter stringFromDate:date];
  109. if ([dateStr isEqualToString:locationString]) {
  110. [formatter setDateFormat:@"MM-dd"];
  111. dateStr = [formatter stringFromDate:date];
  112. NSMutableString *mutString = [NSMutableString stringWithFormat:@"%@",dateStr];
  113. NSString *str = [dateStr substringWithRange:NSMakeRange(3, 1)];
  114. if ([str isEqualToString:@"0"]) {
  115. [mutString replaceCharactersInRange:NSMakeRange(3, 1) withString:@""];
  116. }
  117. NSString *str2 = [mutString substringWithRange:NSMakeRange(0, 1)];
  118. if ([str2 isEqualToString:@"0"]) {
  119. [mutString replaceCharactersInRange:NSMakeRange(0, 1) withString:@""];
  120. }
  121. return mutString;
  122. }else{
  123. [formatter setDateFormat:@"yyyy-MM-dd"];
  124. dateStr = [formatter stringFromDate:date];
  125. return dateStr;
  126. }
  127. }
  128. }
  129. return @"";
  130. }
  131. + (NSInteger)howManyDaysInThisYear:(NSInteger)year withMonth:(NSInteger)month{
  132. //下面这种频繁切换月份时候易崩溃
  133. // NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
  134. // [formatter setDateFormat:@"yyyy-MM"]; // 年-月
  135. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*60*60]];
  136. // NSString * dateStr = [NSString stringWithFormat:@"%ld-%ld",(long)year,(long)month];
  137. //
  138. // NSDate * date = [formatter dateFromString:dateStr];
  139. //
  140. // //
  141. // NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  142. //
  143. // NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
  144. // inUnit: NSCalendarUnitMonth
  145. // forDate:date];
  146. // return range.length;
  147. if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)){
  148. return 31;
  149. } else if((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
  150. return 30;
  151. } else { //2月
  152. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
  153. return 29;
  154. }else{
  155. return 28;
  156. }
  157. }
  158. }
  159. @end