ios所有的时间格式转化,你能使用到的都在这里
前一段时间,公司要求传递各种时间类型,以及各种时间戳的转化,因此,自己抽了一点时间去转化这个时间类型,最后,把大部分能用到的时间类型,写在了时间的string类型中,进行了分类的合并,现在有时间,给大家展示一下代码。欢迎指正
下面是对这些类型文件的详细解决方法,没有写的再次封装返回,大家将就着看就是
//
// FSLTimeDate.m
// DaZhanGui
//
// Created by 世霖 on 2017/3/24.
// Copyright © 2017年 zhu peitong. All rights reserved.
//
#import "FSLTimeDate.h"
@implementation FSLTimeDate
+(NSString *)timeDateBackYMD:(NSString *)deepTime{
//首先拿到当前的时间戳。
NSString * timestamp = [NSString stringWithFormat:@"%@",deepTime];
NSDateFormatter *formatter =[[NSDateFormatter alloc]init];
NSTimeZone *timeZone =[NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSDate * currentDate = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]/1000.0];
NSString * formDateStr = [formatter stringFromDate:currentDate];
return formDateStr;
}
+(NSString *)timeDateBackYMDHMS:(NSString *)deepTime{
//首先拿到当前的时间戳。
NSString * timestamp = [NSString stringWithFormat:@"%@",deepTime];
NSDateFormatter *formatter =[[NSDateFormatter alloc]init];
// NSTimeZone *timeZone =[NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
NSTimeZone *timeZone =[NSTimeZone systemTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
NSDate * currentDate = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]/1000.0];
NSString * formDateStr = [formatter stringFromDate:currentDate];
return formDateStr;
}
+(NSString *)timeDateBackHMS:(NSString *)deepTime
{
//hahhahhahah
//首先拿到当前的时间戳。
NSString * timestamp = [NSString stringWithFormat:@"%@",deepTime];
NSDateFormatter *formatter =[[NSDateFormatter alloc]init];
NSTimeZone *timeZone =[NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"HH:mm"];
NSDate * currentDate = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]/1000.0];
NSString * formDateStr = [formatter stringFromDate:currentDate];
return formDateStr;
}
+(NSString *)timeDateBackYMDHMSAndWeek:(NSString *)deepTime{
NSString * timestamp = [NSString stringWithFormat:@"%@",deepTime];
NSDateFormatter *formatter =[[NSDateFormatter alloc]init];
NSTimeZone *timeZone =[NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
NSDate * currentDate = [NSDate dateWithTimeIntervalSince1970:[timestamp longLongValue]/1000.0];
NSString * formDateStr = [formatter stringFromDate:currentDate];
#pragma mark-这个是时间日期的转化
NSArray *weekDay = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday ;
comps = [calendar components:unitFlags fromDate:currentDate];
NSInteger week = [comps weekday];
NSLog(@"****%ld",(long)week);
// return [weekDay objectAtIndex:(week-1)];
return [NSString stringWithFormat:@"%@ %@",formDateStr,[weekDay objectAtIndex:(week-1)]];
}
+ (NSInteger)compareDate:(NSString*)aDate withDate:(NSString*)bDate;
{
NSInteger aa;
NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformater setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *dta = [[NSDate alloc] init];
NSDate *dtb = [[NSDate alloc] init];
dta = [dateformater dateFromString:aDate];
dtb = [dateformater dateFromString:bDate];
NSComparisonResult result = [dta compare:dtb];
/** {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending}; */
switch (result) {
case NSOrderedSame:
aa=0;
break;
case NSOrderedAscending:
aa=1;
break;
case NSOrderedDescending:
aa=-1;
break ;
default: NSLog(@"erorr dates %@, %@", bDate, aDate);break;
break;
}
return aa;
}
/** 必须严格确定转换的格式 */
+(NSString *)timeDateBackStandardTimeTamp:(NSString *)deepTime{
NSString* timeStr =deepTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
NSTimeZone* timeZone = [NSTimeZone systemTimeZone];
// timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:timeStr];
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
// NSTimeInterval interval = [confromTimesp timeIntervalSince1970];
long long totalMilliseconds = timeSp*1000.0 ;
NSLog(@"totalMilliseconds=%llu",totalMilliseconds);
NSString *timeString = [NSString stringWithFormat:@"%lld" ,totalMilliseconds];
return timeString;
}
+(NSString *)timeDateBackTodyDate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yy-MM-dd"];
NSString*dateTime = [formatter stringFromDate:[NSDate date]];
return dateTime;
}
/** 拿到开始的时间信息 */
+(NSDate *)timeDateBackFirstDateOfMouth{
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal
components:NSYearCalendarUnit | NSMonthCalendarUnit
fromDate:now];
comps.day = 1;
NSDate *firstDay = [cal dateFromComponents:comps];
return firstDay;
}
/** 拿到结束的时间信息 */
+(NSDate *)timeDateBackLastDateOfMouth{
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal
components:NSYearCalendarUnit | NSMonthCalendarUnit
fromDate:now];
comps.day = 1;
NSDate *firstDay = [cal dateFromComponents:comps];
//[FSLTimeDate ]
NSDateComponents *comps2 = [cal
components:NSYearCalendarUnit | NSMonthCalendarUnit
fromDate:now];
comps2.day = 31;
NSDate *lastDay = [cal dateFromComponents:comps2];
return lastDay;
}
+(NSString *)timeDateBackMouth{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMM"];
NSString*dateTime = [formatter stringFromDate:[NSDate date]];
return dateTime;
}
@end