iOS开发中需要的时间格式的转换
2016-04-20 11:05
465 查看
/**
* App内关于运动信息显示的[运动花费时间]格式化方法
*
* @param costTimeInt 运动花费时间数值(秒)
*
* @return 格式化后的花费时间字符串(00:00:00)
*/
+(NSString *)formatTrainDuration:(NSInteger)duration;
/**
* 获取当前系统时间戳
*
* @return
*/
+ (NSString *) recordTime;
/**
* 日期格式转为字符串格式
*
* @return yyyy-MM-dd HH:mm:ss
*/
- (NSString *) dateToString;
/**
* 今年的时间不显示年份,不是今年的显示年份
*/
+ (NSString )dateOfChina:(NSString )inputDate;
/**
* 日期格式:转yyyy-mm-dd 成yyyy.mm.dd
*
* @param dateString
*
* @return
*/
+(NSString )datewithDot:(NSString )dateString;
/**
* 根据用户设定的时分转换成推送时间(格林时间)
*
* @param time (10:00)
*
* @return (2016-01-01 10:00 》2016-01-01 02:00 )
*/
+(NSDate )fireDate:(NSString )time;
/**
* 根据日期返回当前是否是周末
*
* @param inputDate
*
* @return Y:周末,N:周一~周五
*/
+ (BOOL)isWeekend:(NSDate*)inputDate;
/**
* App内关于运动信息显示的[运动花费时间]格式化方法
*
* @param costTimeInt 运动花费时间数值(秒)
*
* @return 格式化后的花费时间字符串(00:00:00)
*/
+(NSString *)formatTrainDuration:(NSInteger)duration {
}
/**
* 获取当前系统时间戳
*
* @return
*/
+ (NSString *) recordTime {
//获取当前系统时间戳
UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
return [NSString stringWithFormat:@”%llu”,recordTime];
}
/**
* 日期格式转为字符串格式
*
* @return yyyy-MM-dd HH:mm:ss
*/
- (NSString *) dateToString {
//转换时间格式
NSDateFormatter*df = [[NSDateFormatter alloc]init];//格式化
[df setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
return [df stringFromDate:self];
}
/**
* 今年的时间不显示年份,不是今年的显示年份
*/
+ (NSString )dateOfChina:(NSString )inputDate {
if ([inputDate length] >=18) {
NSDate *publishDate = [self dateFormatFull:[inputDate stringByReplacingOccurrencesOfString:@”T” withString:@” “] withFullDate:YES];
}
+(NSString *)getCurrentDateTime
{
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
//convert the format to yyyy-mm-ddThh:mm:ss,example:2010-02-18T17:02:36
NSString *currentDateString = [[outputFormatter stringFromDate:currentDate] stringByReplacingCharactersInRange:NSMakeRange(10, 1) withString:@”T”];
return currentDateString;
}
//字符串日期格式化
+(NSString )stringDateFormat:(NSString )date{
if(date==nil||date.length<16){
return date;
}
}
+(NSDate *)sysFormatDate
{
NSString *formatDateStr = [[NSDate date] dateToString];
}
+(NSDate )dateFormatFull:(NSString )dateString
withFullDate:(BOOL)fullDate
{
NSDate *newDate = nil;
if ([NSString isNotEmpty:dateString]) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *oldDate;
if (fullDate) {
[dateFormatter setDateFormat: @”yyyy-MM-dd HH:mm:ss”];
oldDate=[dateFormatter dateFromString:[dateString substringToIndex:19]];
} else {
[dateFormatter setDateFormat: @”yyyy-MM-dd”];
oldDate=[dateFormatter dateFromString:[dateString substringToIndex:10]];
}
NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];
}
}
/**
* 日期格式:转yyyy-mm-dd 成yyyy.mm.dd
*
* @param dateString
*
* @return
*/
+(NSString )datewithDot:(NSString )dateString{
NSString *strDate;
if (dateString.length>=10) {
dateString = [dateString substringToIndex:10];
strDate = [dateString stringByReplacingOccurrencesOfString:@”-” withString:@”.”];
}
return strDate;
}
/**
* [本地推送用]根据用户设定的时分转换成推送时间(格林时间)
*
* @param time (10:00)
*
* @return (2016-01-01 10:00 》2016-01-01 02:00 )
*/
+(NSDate )fireDate:(NSString )time {
NSDate *newDate = nil;
if ([NSString isNotEmpty:time]) {
NSDateFormatter *dateDay = [[NSDateFormatter alloc]init];
[dateDay setDateFormat:@”yyyy-MM-dd”];
NSString *systemDate = [dateDay stringFromDate:[NSDate date]];
//截10位string的时间
time = [NSString stringWithFormat:@”%@ %@”,[systemDate substringToIndex:10],time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @”yyyy-MM-dd HH:mm”];
newDate=[dateFormatter dateFromString:time];
}
}
/**
* 根据日期返回当前是否是周末
*
* @param inputDate
*
* @return Y:周末,N:周一~周五
*/
+ (BOOL)isWeekend:(NSDate*)inputDate {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
}
@end
* App内关于运动信息显示的[运动花费时间]格式化方法
*
* @param costTimeInt 运动花费时间数值(秒)
*
* @return 格式化后的花费时间字符串(00:00:00)
*/
+(NSString *)formatTrainDuration:(NSInteger)duration;
/**
* 获取当前系统时间戳
*
* @return
*/
+ (NSString *) recordTime;
/**
* 日期格式转为字符串格式
*
* @return yyyy-MM-dd HH:mm:ss
*/
- (NSString *) dateToString;
/**
* 今年的时间不显示年份,不是今年的显示年份
*/
+ (NSString )dateOfChina:(NSString )inputDate;
/**
* 日期格式:转yyyy-mm-dd 成yyyy.mm.dd
*
* @param dateString
*
* @return
*/
+(NSString )datewithDot:(NSString )dateString;
/**
* 根据用户设定的时分转换成推送时间(格林时间)
*
* @param time (10:00)
*
* @return (2016-01-01 10:00 》2016-01-01 02:00 )
*/
+(NSDate )fireDate:(NSString )time;
/**
* 根据日期返回当前是否是周末
*
* @param inputDate
*
* @return Y:周末,N:周一~周五
*/
+ (BOOL)isWeekend:(NSDate*)inputDate;
import “NSDate+Category.h”
@implementation NSDate(Category)/**
* App内关于运动信息显示的[运动花费时间]格式化方法
*
* @param costTimeInt 运动花费时间数值(秒)
*
* @return 格式化后的花费时间字符串(00:00:00)
*/
+(NSString *)formatTrainDuration:(NSInteger)duration {
// format of hour NSString *str_hour = @""; if (duration/3600 > 0) { str_hour = [NSString stringWithFormat:@"%02ld:",duration/3600]; } // format of minute NSString *str_minute = [NSString stringWithFormat:@"%02ld",(duration%3600)/60]; // format of second NSString *str_second = [NSString stringWithFormat:@"%02ld",duration%60]; // format of time NSString *format_time = [NSString stringWithFormat:@"%@%@:%@",str_hour,str_minute,str_second]; return format_time;
}
/**
* 获取当前系统时间戳
*
* @return
*/
+ (NSString *) recordTime {
//获取当前系统时间戳
UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
return [NSString stringWithFormat:@”%llu”,recordTime];
}
/**
* 日期格式转为字符串格式
*
* @return yyyy-MM-dd HH:mm:ss
*/
- (NSString *) dateToString {
//转换时间格式
NSDateFormatter*df = [[NSDateFormatter alloc]init];//格式化
[df setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
return [df stringFromDate:self];
}
/**
* 今年的时间不显示年份,不是今年的显示年份
*/
+ (NSString )dateOfChina:(NSString )inputDate {
if ([inputDate length] >=18) {
NSDate *publishDate = [self dateFormatFull:[inputDate stringByReplacingOccurrencesOfString:@”T” withString:@” “] withFullDate:YES];
NSTimeInterval late = [publishDate timeIntervalSince1970]; NSDate* dat = [self sysFormatDate]; NSTimeInterval now=[dat timeIntervalSince1970]; // 时间差 NSTimeInterval timeDiff = now-late; // 算出时间差的天数 long day = timeDiff/(24*60*60); // 算出时间差的小时数 long hour = (timeDiff/(60*60)-day*24); // 算出时间差的分钟数 long min = timeDiff/60-day*24*60-hour*60; // HH:mm NSString *hourMm = [inputDate substringWithRange:NSMakeRange(11,5)]; // 当前系统时间 NSString *sysHourMm = [[self getCurrentDateTime] substringWithRange:NSMakeRange(11,8)]; // YYYY NSString *yearStr = [inputDate substringWithRange:NSMakeRange(0,4)]; // 当前系统年份 NSString *sysYear = [[self getCurrentDateTime] substringWithRange:NSMakeRange(0,4)]; if (![yearStr isEqualToString:sysYear]) { if (inputDate==nil||inputDate.length<16) { return inputDate; } NSString *timeS = [inputDate substringWithRange:NSMakeRange(11, 5)]; return [NSString stringWithFormat:@"%@年%@月%@日 %@",[inputDate substringWithRange:NSMakeRange(0, 4)],[inputDate substringWithRange:NSMakeRange(5, 2)],[inputDate substringWithRange:NSMakeRange(8, 2)],timeS]; } if (day <= 0) { // 时间差24小时内 if (hour <= 0) { // 时间差1小时内 if (min > 0) { return [NSString stringWithFormat:@"%@分钟前",[NSNumber numberWithInteger:min]]; } // 时间差一分钟内 return [NSString stringWithFormat:@"刚刚"]; } else { // 时间差超过1小时 NSInteger dayFlg = hour-[[sysHourMm substringWithRange:NSMakeRange(0,2)] intValue]; if (dayFlg <= 0) { if (dayFlg == 0) { NSInteger dayMin = min-[[sysHourMm substringWithRange:NSMakeRange(3,2)] intValue]; if (dayMin <= 0) { NSString *sysDay = [[self getCurrentDateTime] substringWithRange:NSMakeRange(8,2)]; NSString *inputDay = [[self stringDateFormat:inputDate] substringWithRange:NSMakeRange(3, 2)]; if (![sysDay isEqualToString:inputDay]) { return [NSString stringWithFormat:@"昨天 %@",hourMm]; } return [NSString stringWithFormat:@"%@",hourMm]; } else { return [NSString stringWithFormat:@"昨天 %@",hourMm]; } } else { return [NSString stringWithFormat:@"%@",hourMm]; } } else { return [NSString stringWithFormat:@"昨天 %@",hourMm]; } } } } return [self stringDateFormat:inputDate];
}
+(NSString *)getCurrentDateTime
{
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
//convert the format to yyyy-mm-ddThh:mm:ss,example:2010-02-18T17:02:36
NSString *currentDateString = [[outputFormatter stringFromDate:currentDate] stringByReplacingCharactersInRange:NSMakeRange(10, 1) withString:@”T”];
return currentDateString;
}
//字符串日期格式化
+(NSString )stringDateFormat:(NSString )date{
if(date==nil||date.length<16){
return date;
}
NSString *timeS = [date substringWithRange:NSMakeRange(11, 5)]; if ([@"00:00" isEqualToString:timeS]) { timeS = @""; } return [NSString stringWithFormat:@"%@月%@日 %@",[date substringWithRange:NSMakeRange(5, 2)],[date substringWithRange:NSMakeRange(8, 2)],timeS];
}
+(NSDate *)sysFormatDate
{
NSString *formatDateStr = [[NSDate date] dateToString];
return [self dateFormatFull:formatDateStr withFullDate:YES];
}
+(NSDate )dateFormatFull:(NSString )dateString
withFullDate:(BOOL)fullDate
{
NSDate *newDate = nil;
if ([NSString isNotEmpty:dateString]) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *oldDate;
if (fullDate) {
[dateFormatter setDateFormat: @”yyyy-MM-dd HH:mm:ss”];
oldDate=[dateFormatter dateFromString:[dateString substringToIndex:19]];
} else {
[dateFormatter setDateFormat: @”yyyy-MM-dd”];
oldDate=[dateFormatter dateFromString:[dateString substringToIndex:10]];
}
NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];
}
return newDate;
}
/**
* 日期格式:转yyyy-mm-dd 成yyyy.mm.dd
*
* @param dateString
*
* @return
*/
+(NSString )datewithDot:(NSString )dateString{
NSString *strDate;
if (dateString.length>=10) {
dateString = [dateString substringToIndex:10];
strDate = [dateString stringByReplacingOccurrencesOfString:@”-” withString:@”.”];
}
return strDate;
}
/**
* [本地推送用]根据用户设定的时分转换成推送时间(格林时间)
*
* @param time (10:00)
*
* @return (2016-01-01 10:00 》2016-01-01 02:00 )
*/
+(NSDate )fireDate:(NSString )time {
NSDate *newDate = nil;
if ([NSString isNotEmpty:time]) {
NSDateFormatter *dateDay = [[NSDateFormatter alloc]init];
[dateDay setDateFormat:@”yyyy-MM-dd”];
NSString *systemDate = [dateDay stringFromDate:[NSDate date]];
//截10位string的时间
time = [NSString stringWithFormat:@”%@ %@”,[systemDate substringToIndex:10],time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @”yyyy-MM-dd HH:mm”];
newDate=[dateFormatter dateFromString:time];
}
NSTimeInterval notificationInterval = [newDate timeIntervalSinceReferenceDate];
NSTimeInterval systemInterval = [NSDate timeIntervalSinceReferenceDate];
// 如果用户设定的时间已经过去,则从明天开始
if (notificationInterval < systemInterval) {
notificationInterval = notificationInterval + 24*60*60;
return [NSDate dateWithTimeIntervalSinceReferenceDate:notificationInterval];
}
return newDate;
}
/**
* 根据日期返回当前是否是周末
*
* @param inputDate
*
* @return Y:周末,N:周一~周五
*/
+ (BOOL)isWeekend:(NSDate*)inputDate {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit; NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate]; switch (theComponents.weekday) { case 7: case 1: return YES; case 2: case 3: case 4: case 5: case 6: default: break; } return NO;
}
@end
相关文章推荐
- Xcode真机测试could not find developer disk image解决方法(支持iOS9.2)
- iOS开发中字符串的截取
- ios之域名转IP和获取IP
- iOS推送证书从申请到使用
- iOS开发中时间格式的转化
- iOS Xcode Server持续集成(二)
- ios开发多线程篇——线程间的通信
- iOS 集成极光推送(一)
- xcode下载iOS开发文档
- iOS-国家代码选择功能github开源分享
- iOS 微博第三方登录的简单实现
- 30分钟搞定iOS自定义相机
- iOS 处理第三方库冲突
- ios开发多线程篇——线程安全
- IOS开发之——自定义导航控制器
- iOS NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- 欢迎使用CSDN-markdown编辑器
- iOS 直播技术平台
- 关于键盘上方创建返回按钮
- 极验geetest IOS端开发文档