iOS时间戳转换日期 iOS时间戳转换星期几

一、根据long类型 时间戳 转换周几


转自: http://www.ithao123.cn/content-7273821.html

写项目过程中,遇到一个问题,要求我把一个时间戳给显示为周几的样式, 百度了一下,完成之后就写一个博客记录下来. 下面是代码

//根据时间戳获取星期几
+ (NSString *)getWeekDayFordate:(long long)data
{
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];

    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:data];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:newDate];

    NSString *weekStr = [weekday objectAtIndex:components.weekday];
    return weekStr;
}


二、根据NSString类型时间戳转换为日期格式

+ (NSString *)getDateAccordingTime:(NSString *)aTime formatStyle:(NSString *)formate{

    NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:[aTime intValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:formate];
    return[formatter stringFromDate:nowDate];
}

PS: formate 返回时可以写成 @"YYYY-MM-HH HH:MM:SS"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值