//时间---->时间戳 时间格式2017-06-09 10:00
+(NSString *)transTotimeSp:(NSString *)time{
//设置转换格式
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//NSString转NSDate
NSDate *date=[formatter dateFromString:time];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
NSLog(@"时间戳%@",timeSp);
return timeSp;
}
- 时间戳--->几月几日. 转化为时间格式MM月dd日
+(NSString *)transToDate:(NSString *)timsp{
NSTimeInterval time=[timsp doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"MM月dd日"];
NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
return currentDateStr;
}
+(NSString *)transToMinDate:(NSString *)timsp{
NSTimeInterval time=[timsp doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"HH:mm"];
NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
return currentDateStr;
}
- 时间戳--->传入开始时间加时长 转化为时间格式(01月,01日,10:00-12:00,星期一)
//时间戳--->月 日 时间。星期
+(void)transFormat:(NSNumber *)format hour:(NSNumber *)hour callBack:(void (^)(NSString *, NSString *, NSString *, NSString *))callback{
NSTimeInterval time=[format doubleValue] ;//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
// NSDateComponent 可以获得日期的详细信息,即日期的组成
NSString *weekStr=nil;
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday fromDate:detaildate];
NSDate *detaildate1=[NSDate dateWithTimeIntervalSince1970:(time + hour.intValue*60*60)];
// NSDateComponent 可以获得日期的详细信息,即日期的组成
NSDateComponents *comps1 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday fromDate:detaildate1];
if(comps.weekday==1)
{
weekStr=@"星期日";
}else if(comps.weekday==2){
weekStr=@"星期一";
}else if(comps.weekday==3){
weekStr=@"星期二";
}else if(comps.weekday==4){
weekStr=@"星期三";
}else if(comps.weekday==5){
weekStr=@"星期四";
}else if(comps.weekday==6){
weekStr=@"星期五";
}else if(comps.weekday==7){
weekStr=@"星期六";
}
callback([NSString stringWithFormat:@"%02ld",comps.month],[NSString stringWithFormat:@"%02ld",comps.day],[NSString stringWithFormat:@"%02ld:%02ld-%02ld:%02ld",comps.hour,comps.minute,comps1.hour,comps1.minute],weekStr);
}
- 传入年月日,返回年龄 传入格式yyyy-MM-dd
+(int)getAgeWithBirthDay:(NSString *)birthDate{
//首先把string类型的birth转为nsdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
//出生日期
NSDate *birth = [dateFormatter dateFromString:birthDate];
//现在的日期
NSString *currenDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSDate *today = [dateFormatter dateFromString:currenDateStr];
//从出生到现在过去了多久
NSTimeInterval time = [today timeIntervalSinceDate:birth];
int age = ((int)time)/(3600*24*365);
return age;
}
+ (NSString *)getConstellationFromBirthday:(NSDate*)birthday{
NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comp1 = [myCal components:NSCalendarUnitMonth| NSCalendarUnitDay fromDate:birthday];
NSInteger month = [comp1 month];
NSInteger day = [comp1 day];
NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
NSString *astroFormat = @"102123444543";
NSString *result;
if (month <1|| month>12|| day <1|| day>31){
return @"错误日期格式!";
}
if(month ==2 && day>29)
{
return @"错误日期格式!!";
}else if(month ==4 || month ==6 || month ==9 || month ==11) {
if (day>30) {
return @"错误日期格式!!!";
}
}
result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];
return [result stringByAppendingString:@"座"];
}