iOS时间戳转换日期 iOS时间戳转换星期几
2015-12-07 11:31
495 查看
一、根据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"
转自: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"
相关文章推荐
- iOS注册,找回密码时用到的获取验证码
- iOS基础-属性与实例变量区别
- 使用AudioSession实现音乐后台播放功能
- Mina源码阅读笔记(五)—Mina对连接的操作IoSession
- IOS获取时间戳并转化为对应格式的时间
- iOS判断字符串是否包含子字符串
- iOS获取设备标识符、型号等信息
- IOS开发:对TextField或TextView添加文字限制
- Ios和tvos按需请求资源简介
- iOS-获取Wifi的IP和名字
- iOS开发 - CocoaPods的使用
- IOS关闭虚拟键盘
- iOS 打包ipa 教程
- 变量重复定义的奇怪bug
- iOS 9 loadViewIfNeeded的使用
- iOS 笔记-自定义的导航栏按钮
- iOS之Xcode设置点滴记录(二)
- IOS 导航条 以及 状态栏颜色 相关设置
- iOS之__block和__weak使用
- iOS 21种设计模式之抽象工厂(Abstract Factory)