嗨般贼,時(shí)間好快,馬上又要春季了...
話不多說 . . .
話說工作之余 . . . 其實(shí)是項(xiàng)目經(jīng)常用到的一個(gè)整合吧 . . .
決定把這個(gè)整合分享給大家 . . .
勿噴 . . . 只是對(duì)自己項(xiàng)目常用的整合出來分享給大家 . . .
希望能夠快速幫助到大家 . . .
如若有更好的方法歡迎多多指教. . .
喜歡的朋友們可以點(diǎn)點(diǎn)贊打打賞加加好友常常交流共勉一起進(jìn)步咯
//
// NSDate+Util.h
// zxl
//
// Created by zxl on 17/1/9.
// Copyright ? 2016年 zxl. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDate (Util)
///當(dāng)前時(shí)間時(shí)間戳
+(NSInteger)getNowTimeInterval;
///時(shí)間戳轉(zhuǎn)字符串
+(NSString *)timeIntervalToString:(NSInteger)timeInterval;
///字符串轉(zhuǎn)時(shí)間戳
+(NSInteger)stringToTimeInterval:(NSString *)string;
///字符串轉(zhuǎn)NSDate
+(NSDate *)stringToDate:(NSString *)string;
///NSDate轉(zhuǎn)字符串
+(NSString *)dateToString:(NSDate *)date;
///以前時(shí)間距離現(xiàn)在多久
+(NSString *)timeFromToNow:(NSInteger)formerTimeInterval;
///將來時(shí)間距離現(xiàn)在多久
+(NSString *)willTimeToNow:(NSInteger)willTimeInterval;
/// 今天奥吩、明天哼蛆、后天的最晚時(shí)間戳
+(NSInteger)indexDayTimeInterval:(NSInteger)dayCount
@end
//
// NSDate+Util.m
// zxl
//
// Created by zxl on 17/1/9.
// Copyright ? 2016年 zxl. All rights reserved.
//
#import "NSDate+Util.h"
@implementation NSDate (Util)
#pragma mark 當(dāng)前時(shí)間時(shí)間戳
+(NSInteger)getNowTimeInterval
{
NSDate *date = [NSDate date];
NSInteger timeInterval = [date timeIntervalSince1970];
return timeInterval;
}
#pragma mark 時(shí)間戳轉(zhuǎn)字符串
+(NSString *)timeIntervalToString:(NSInteger)timeInterval
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *string = [dateFormatter stringFromDate:date];
return string;
}
#pragma mark 字符串轉(zhuǎn)時(shí)間戳
+(NSInteger)stringToTimeInterval:(NSString *)string
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:string];
NSInteger timeInterval = [date timeIntervalSince1970];
return timeInterval;
}
#pragma mark 字符串轉(zhuǎn)NSDate
+(NSDate *)stringToDate:(NSString *)string
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:string];
return date;
}
#pragma mark NSDate轉(zhuǎn)字符串
+(NSString *)dateToString:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *string = [dateFormatter stringFromDate:date];
return string;
}
#pragma mark 以前時(shí)間距離現(xiàn)在多久
+(NSString *)timeFromToNow:(NSInteger)formerTimeInterval
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:formerTimeInterval];
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
timeInterval = -timeInterval;
long temp = 0;
NSString *result;
if (timeInterval < 0) {//將來時(shí)間
result = [self willTimeToNow:formerTimeInterval];
}
else if (timeInterval < 60) {
result = [NSString stringWithFormat:@"剛剛"];
}
else if((temp = timeInterval/60) < 60){
result = [NSString stringWithFormat:@"%ld分鐘前",temp];
}
else if((temp = temp/60) < 24){
result = [NSString stringWithFormat:@"%ld小時(shí)前",temp];
}
else if((temp = temp/24) < 30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/30) < 12){
result = [NSString stringWithFormat:@"%ld個(gè)月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
#pragma mark 將來時(shí)間距離現(xiàn)在多久
+(NSString *)willTimeToNow:(NSInteger)willTimeInterval
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:willTimeInterval];
//方法一
//NSDate *willDate = [[NSDate date] laterDate:date];
//NSTimeInterval timeInterval = [willDate timeIntervalSinceNow];
//方法二
NSTimeInterval timeInterval = [date timeIntervalSinceDate:[NSDate date]];
long temp = 0;
NSString *result;
if (timeInterval < 0) {//以前時(shí)間
result = [self timeFromToNow:willTimeInterval];
}
else if (timeInterval < 60) {
result = [NSString stringWithFormat:@"剛剛"];
}
else if((temp = timeInterval/60) < 60){
result = [NSString stringWithFormat:@"%ld分鐘后",temp];
}
else if((temp = temp/60) < 24){
result = [NSString stringWithFormat:@"%ld小時(shí)后",temp];
}
else if((temp = temp/24) < 30){
result = [NSString stringWithFormat:@"%ld天后",temp];
}
else if((temp = temp/30) < 12){
result = [NSString stringWithFormat:@"%ld個(gè)月后",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年后",temp];
}
return result;
}
#pragma mark 今天、明天霞赫、后天的最晚時(shí)間戳
+(NSInteger)indexDayTimeInterval:(NSInteger)dayCount
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
[components setDay:([components day]+dayCount)];
//當(dāng)前日期
NSDate *indexDate = [calendar dateFromComponents:components];
//日期轉(zhuǎn)字符串
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dayString = [dateFormatter stringFromDate:indexDate];
//字符串日期+一天最晚時(shí)間
NSString *end = [NSString stringWithFormat:@"%@ 23:59:59",dayString];
//字符串轉(zhuǎn)時(shí)間戳
NSDateFormatter *endFormatter = [[NSDateFormatter alloc] init];
[endFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *endDate = [endFormatter dateFromString:end];
NSInteger endTimeInterval = [endDate timeIntervalSince1970];
return endTimeInterval;
}
@end