hg888皇冠手机登录

【www.hg888.com】objectC时间用法

十一月 30th, 2019  |  www.hg888.com

iOS中国和东瀛期和字符串的相互影响转变(特殊卡塔尔国

例1、 从服务器段获取到的字符串转变为时间如:转变141688271二零零四

//互联网央浼获取的多少

NSString *time =
[NSStringstringWithFormat:@%@,[[dateListobjectAtIndex:indexPath.row]gradeDate]];

NSInteger num = [time integerValue]/1000;(重点)

NSDateFormatter *formatter =
[[[NSDateFormatteralloc]init]autorelease];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:@YYYY-MM-dd];

NSDate*confromTimesp =
[NSDatedateWithTimeIntervalSince1970:num];NSString*confromTimespStr =
[formatterstringFromDate:confromTimesp];

cell.DateContent.text = confromTimespStr;

中间转播之后结果为:二〇一六-11-25

 

例2、如何怎么样将多个字符串如“
20130826134106”装化为随意的日猪时间格式,上边列举两种类型

NSString*string = @20110826134106;

NSDateFormatter *inputFormatter= [[[NSDateFormatter alloc] init]
autorelease];

[inputFormatter setLocale:[[[NSLocale alloc]
initWithLocaleIdentifier:@en_US] autorelease]];

[inputFormatter setDateFormat:@yyyyMMddHHmmss];

NSDate*inputDate = [inputFormatter dateFromString:string];

NSLog(@date= %@, inputDate);

 

NSDateFormatter *outputFormatter= [[[NSDateFormatter alloc] init]
autorelease];

[outputFormatter setLocale:[NSLocale currentLocale]];

[outputFormatter setDateFormat:@yyyy年MM月dd日 HH时mm分ss秒];

NSString *str= [outputFormatter stringFromDate:inputDate];

NSLog(@testDate:%@,str);

五回打字与印刷的结果为:

date= 2011-08-26 05:41:06 +0000

testDate:2011年08月26日 13时41分06秒

 

 

卡塔尔(قطر‎ 例1、
从服务器段获取到的字符串转变为时间如:转化141688271贰零零零//网络诉求获取的多寡 NSString *time =…

1、怎么样怎么样将三个字符串如“
二零一二0826134106”装化为专断的日期时间格式,上边罗列两种档案的次序:

#define kDEFAULT_DATE_TIME_FORMAT (@”yyyy-MM-dd HH:mm:ss”)

“`

//获取当前不久期,时间
+(NSDate *)getCurrentDate{
    NSDate *now = [NSDate date];
    return now;
}

NSString* string = @”20110826134106″;

//将日期转变为字符串(日期,时间)
+(NSString *)getDateStringFromDate:(NSDate *)date{
    NSInteger location = 0;
    NSString *timeStr = @””;
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]
autorelease];
    [formatter setLocale:[[[NSLocale alloc]
initWithLocaleIdentifier:@”en_US”] autorelease]];
    [formatter setDateFormat:@”HH:mm:a”];
    NSString *ampm = [[[formatter stringFromDate:date]
componentsSeparatedByString:@”:”] objectAtIndex:2];
    timeStr = [formatter stringFromDate:date];
    NSRange range = [timeStr rangeOfString:[NSString
stringWithFormat:@”:%@”,ampm]];
    location = range.location;
    NSString *string = [timeStr substringToIndex:location];
    timeStr = [NSString stringWithFormat:@”%@ %@”,ampm,string];
    
    
    NSString *dateStr = @””;
    NSDateFormatter *Dformatter = [[[NSDateFormatter alloc] init]
autorelease];
    [Dformatter setLocale:[[[NSLocale alloc]
initWithLocaleIdentifier:@”en_US”] autorelease]];
    [Dformatter setDateFormat:@”yyyy-MM-dd”];    
    dateStr = [Dformatter stringFromDate:date];
//    NSLog(@”%@”, [NSString
stringWithFormat:@”%@  %@”,dateStr,timeStr]);
    return [NSString stringWithFormat:@”%@  %@”,dateStr,timeStr];
}

NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init]
autorelease];

//总计五个日子之间的出入,过了有些天。。
+(NSInteger)getDateToDateDays:(NSDate *)date withSaveDate:(NSDate
*)saveDate{
    NSCalendar* chineseClendar = [ [ NSCalendar alloc ]
initWithCalendarIdentifier:NSGregorianCalendar ];  
    NSUInteger unitFlags =  NSHourCalendarUnit | NSMinuteCalendarUnit
|   
    NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit |
NSYearCalendarUnit;  
    NSDateComponents *cps = [ chineseClendar components:unitFlags
fromDate:date  toDate:saveDate  options:0];  
    NSInteger diffDay   = [ cps day ];  
    [chineseClendar release]; 
    return diffDay;
}

[inputFormatter setLocale:[[[NSLocale alloc]
initWithLocaleIdentifier:@”en_US”] autorelease]];

例:NSDate *lastDate = [self
getSaveDate];//saveDate通过将NSDate转换为NSString来保存
    currentDate = [NSDate date];
    NSInteger day = [DateHelper getDateToDateDays:currentDate
withSaveDate: lastDate];

[inputFormatter setDateFormat:@”yyyyMMddHHmmss”];

//日期转字符串

NSDate* inputDate = [inputFormatter dateFromString:string];

  • (NSString * )NSDateToNSString: (NSDate * )date
    {    
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
        NSString *dateString = [formatter stringFromDate:date];
        [formatter release];
        return dateString;
    }

NSLog(@”date = %@”, inputDate);

//字符串转日期

NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc]www.hg888.com, init]
autorelease];

  • (NSDate * )NSStringToNSDate: (NSString * )string
    {    
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat: kDEFAULT_DATE_TIME_FORMAT];
        NSDate *date = [formatter dateFromString :string];
        [formatter release];
        return date;
    }

[outputFormatter setLocale:[NSLocale currentLocale]];

//壹玖柒零年到前不久的秒数转变届期间突显

[outputFormatter setDateFormat:@”yyyy年MM月dd日 HH时mm分ss秒”];

  • (NSString *)bySecondGetDate:(NSString *)second{
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@”yyyy年MM月dd日 HH:mm:ss”];
        NSString *dateLoca = [NSString stringWithFormat:@”%@”,second];
        NSTimeInterval time=[dateLoca doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        NSString *timestr = [formatter stringFromDate:detaildate];
        return timestr;
    }

NSString *str = [outputFormatter stringFromDate:inputDate];

NSLog(@”testDate:%@”, str);

“`

四遍打字与印刷的结果为:

date = 2011-08-26 05:41:06 +0000

testDate:2011年08月26日 13时41分06秒

注脚:下边包车型地铁年华是美利坚联邦合众国流年,下边包车型大巴没有安装

NSString* string = @”Wed, 05 May 2011 10:50:00 +0800″;

标签:, , ,

Your Comments

近期评论

    功能


    网站地图xml地图