123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- //
- // JZTime.m
- // JIZHANG
- //
- // Created by kuxuan on 2017/10/24.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "JZTime.h"
- @implementation JZTime
- +(NSString *)getTime{
- NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
- long long int date = (long long int)time;
- NSString *timeString = [NSString stringWithFormat:@"%lld", date];
- return timeString;
- }
- +(NSString *)getYYMMDDWithDate:(NSDate *)date{
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yy-MM-dd"];
- NSString *dateStr = [formatter stringFromDate:date];
- return dateStr;
- }
- +(NSString *)getYYMMDDWithDate2:(NSDate *)date{
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy-MM-dd"];
- NSString *dateStr = [formatter stringFromDate:date];
- return dateStr;
- }
- +(NSString *)getYYMMDDHHMMWithDate:(NSDate *)date{
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy年MM月dd日"];
- NSString *dateStr = [formatter stringFromDate:date];
- return dateStr;
- }
- +(NSString *)getYYMMDDHHMMWithDate2:(NSDate *)date{
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
- NSString *dateStr = [formatter stringFromDate:date];
- return dateStr;
- }
- +(NSString *)getYear:(NSDate *)date
- {
- NSString * dateString = [self getYYMMDDWithDate2:date];
- NSArray *dateArray = [dateString componentsSeparatedByString:@"-"];
- return dateArray[0];
- }
- +(NSString *)getMonth:(NSDate *)date
- {
- NSString * dateString = [self getYYMMDDWithDate2:date];
- NSArray *dateArray = [dateString componentsSeparatedByString:@"-"];
- NSString *twoString = dateArray[1];
- if ([twoString hasPrefix:@"0"]) {
- twoString = [twoString substringFromIndex:1];
- }
-
- return twoString;
- }
- +(NSString *)getTimeFromTimeString:(NSString *)timeString
- {
- NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue/1000];
- NSDate *localDate = [NSDate date];
- NSInteger interval = localDate.timeIntervalSince1970 - date.timeIntervalSince1970;
- interval=interval/60/60/24;
- if (interval<1.0) {
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"HH:mm"];
- NSString *dateStr = [formatter stringFromDate:date];
-
- return dateStr;
- }else if(interval<2.0){
- return @"昨天";
- }else if(interval<3.0){
- return @"前天";
- }else{
-
- //日期
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy-MM-dd"];
- NSString *dateStr = [formatter stringFromDate:date];
-
- return dateStr;
- }
-
- return @"";
- }
- +(NSString *)getTimeWithSice1970TimeString:(NSString *)timeString{
-
- NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/1000];
- NSDate *localDate = [NSDate date];
- NSInteger interval = localDate.timeIntervalSince1970 - date.timeIntervalSince1970;
- interval = interval/60;//分钟为单位
- if (interval <= 60) {
- if (interval <= 30) {
- if (interval == 0){
- return @"刚刚";
- }else{
- return [NSString stringWithFormat:@"%ld分钟前",(long)interval];
- }
- }else if (interval > 30){
- return @"半小时前";
- }
- }else{
- interval = interval/60;
- if (interval <= 24) {
- return [NSString stringWithFormat:@"%ld小时前",(long)interval];
- }else{
-
-
- //今年
- NSDate * senddate=[NSDate date];
-
- NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
-
- [dateformatter setDateFormat:@"YYYY"];
-
- NSString * locationString=[dateformatter stringFromDate:senddate];
-
- //日期
- NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
- [formatter setDateFormat:@"yyyy"];
- NSString *dateStr = [formatter stringFromDate:date];
-
- if ([dateStr isEqualToString:locationString]) {
-
- [formatter setDateFormat:@"MM-dd"];
- dateStr = [formatter stringFromDate:date];
- NSMutableString *mutString = [NSMutableString stringWithFormat:@"%@",dateStr];
- NSString *str = [dateStr substringWithRange:NSMakeRange(3, 1)];
-
- if ([str isEqualToString:@"0"]) {
- [mutString replaceCharactersInRange:NSMakeRange(3, 1) withString:@""];
- }
- NSString *str2 = [mutString substringWithRange:NSMakeRange(0, 1)];
-
- if ([str2 isEqualToString:@"0"]) {
- [mutString replaceCharactersInRange:NSMakeRange(0, 1) withString:@""];
- }
- return mutString;
- }else{
- [formatter setDateFormat:@"yyyy-MM-dd"];
- dateStr = [formatter stringFromDate:date];
- return dateStr;
- }
-
-
- }
- }
-
- return @"";
- }
- + (NSInteger)howManyDaysInThisYear:(NSInteger)year withMonth:(NSInteger)month{
-
- //下面这种频繁切换月份时候易崩溃
- // NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
- // [formatter setDateFormat:@"yyyy-MM"]; // 年-月
- // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8*60*60]];
- // NSString * dateStr = [NSString stringWithFormat:@"%ld-%ld",(long)year,(long)month];
- //
- // NSDate * date = [formatter dateFromString:dateStr];
- //
- // //
- // NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
- //
- // NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
- // inUnit: NSCalendarUnitMonth
- // forDate:date];
- // return range.length;
-
-
- if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)){
- return 31;
- } else if((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
- return 30;
- } else { //2月
- if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
- return 29;
- }else{
- return 28;
- }
- }
- }
- @end
|