在我们开发中,我们经常会遇到将指定时间转为时间戳,下面就是时间转时间戳,-方法或者+方法都可以
/**
字符串转时间戳 如:2017-4-10 17:15:10
@param time 时间
@return 时间戳
*/
- (NSString *)getOtherTimeStrWithString:(NSString *)formatTime{
NSLog(@"formatTime - - - - - -%@",formatTime);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区选择北京时间
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue] * 1000;
NSLog(@"将某个时间转化成 时间戳timeSp:%ld",(long)timeSp); //时间戳的值
return [NSString stringWithFormat:@"%ld",(long)timeSp];
}
将字符串时间转为NSDate
/**
将字符串转成NSDate类型
@param dateString 时间类型的字符串 例如:2018-07-12
@return date
*/
+ (NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *inputDate = [inputFormatter dateFromString:dateString];
//inputDate或出现相差八小时问题,下面是解决相差八小时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: inputDate];
NSDate *localeDate = [inputDate dateByAddingTimeInterval: interval];
return localeDate;
}
获取指定时间的前一天,需要上面的方法协助,也可以直接传入NSDate脱离上面的方法
/**
获取某个时间的前一天
@param date 时间
@return 前一天
*/
+ (NSString *)getLastDayWithDate:(NSString *)timeSring {
NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:[self dateFromString:timeSring]];//前一天
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *strDate = [dateFormatter stringFromDate:lastDay];
return strDate;
}
获取指定时间的后一天,需要上面的方法协助,也可以直接传入NSDate脱离上面的方法
/**
获取某个时间的后一天
@param date 时间
@return 后一天
*/
+ (NSString *)getNextDayWithDate:(NSString *)timeSring {
NSDate *lastDay = [NSDate dateWithTimeInterval:24*60*60 sinceDate:[self dateFromString:timeSring]];//前一天
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *strDate = [dateFormatter stringFromDate:lastDay];
return strDate;
}
获取当前时间
/**
获取当地当前时间
@return 时间字符串
*/
+ (NSString *)getCurrentDateTime {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// yyyy-MM-dd 可自定义,也可以换成 yyyy-MM-dd HH:MM:SS
[formatter setDateFormat:@"yyyy MM-dd"];
NSString *dateTime = [formatter stringFromDate:[NSDate date]];
return dateTime;
}
获取今天是星期几
/**
获取当前是星期几
@return 1、2、3、4、5、6、7(星期六是 7 ,星期日 1)
*/
+ (NSString*)getNowWeekday{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDate *now = [NSDate date];
// 在真机上需要设置区域,才能正确获取本地日期,中国区代码:zh_CN
calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
comps = [calendar components:unitFlags fromDate:now];
NSInteger weekDay = [comps weekday];
return weekDay == 1 ? @"星期日" : weekDay == 2 ? @"星期一" : weekDay == 3 ? @"星期二" : weekDay == 4 ? @"星期三" : weekDay == 5 ? @"星期四" : weekDay == 6 ? @"星期五" : @"星期六" ;
}
判断两个时间的大小,0相等 1 date01小于date02 -1 反之
/************
日期格式请传入:2013-08-05 12:12:12;
如果修改日期格式,比如:2013-08-05,
则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
修改为[df setDateFormat:@"yyyy-MM-dd"];
***********/
+ (int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
int ci;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1 = [df dateFromString:date01];
dt2 = [df dateFromString:date02];
NSComparisonResult result = [dt1 compare:dt2];
switch (result){
case NSOrderedAscending: ci=1; break;
case NSOrderedDescending: ci=-1; break;
case NSOrderedSame: ci=0; break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
return ci;
}
/**
获取过去时间
@return 过去时间
*/
+ (NSString*)gettingCurrentPastTime{
NSDate * mydate = [NSDate date];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy MM-dd"];
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = nil;
comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];
NSDateComponents *adcomps = [[NSDateComponents alloc] init];
[adcomps setYear:0];
//-2代表获取过去两个月的日期,年和日同理
[adcomps setMonth:-2];
[adcomps setDay:0];
NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:mydate options:0];
NSString *beforDate = [dateFormatter stringFromDate:newdate];
return beforDate;
}
传入今天的时间,返回明天的时间
/**
传入今天的时间,返回明天的时间
@param aDate 当前的时间NSDate类型,上面的方法可获取当前时间的NSDate类型
@return 明天时间
*/
+ (NSString *)getTomorrowDay:(NSDate *)aDate {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday |NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
[components setDay:([components day]+1)];
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
[dateday setDateFormat:@"yyyy-MM-dd"];
return [dateday stringFromDate:beginningOfWeek];
}