iOS與日期相關的操作

來源:互聯網
上載者:User

標籤:nsdate   ios   

// Do any additional setup after loading the view, typically from a nib.    //得到當前的日期  注意week1是星期天    NSDate *date = [NSDate date];    NSLog(@"date:%@",date);        //得到(24 * 60 * 60)即24小時之前的日期,dateWithTimeIntervalSinceNow:    NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24* 60* 60)];    NSLog(@"yesterday:%@",yesterday);            NSDateFormatter *formatter =[[NSDateFormatter alloc] init];    //NSDate *date_ = [NSDate date];    [formatter setTimeStyle:NSDateFormatterMediumStyle];    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];        NSDateComponents *comps = [[NSDateComponents alloc] init];    NSInteger unitFlags = NSYearCalendarUnit |    NSMonthCalendarUnit |    NSDayCalendarUnit |    NSWeekdayCalendarUnit |    NSHourCalendarUnit |    NSMinuteCalendarUnit |    NSSecondCalendarUnit;        //int week=0;week1是星期天,week7是星期六;    comps = [calendar components:unitFlags fromDate:date];    int week = [comps weekday];    int year=[comps year];    int month = [comps month];    int day = [comps day];        //[formatter setDateStyle:NSDateFormatterMediumStyle];        //This sets the label with the updated time.        int hour = [comps hour];    int min = [comps minute];    int sec = [comps second];    NSLog(@"week:%d : %@",week,[self week:week]);    NSLog(@"year:%d",year);    NSLog(@"month:%d",month);    NSLog(@"day:%d",day);    NSLog(@"hour:%d",hour);    NSLog(@"min:%d",min);    NSLog(@"sec:%d",sec);        //得到毫秒    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];    //[dateFormatter setDateFormat:@"hh:mm:ss"]    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];    NSLog(@"Date:%@", [dateFormatter stringFromDate:[NSDate date]]);


-(NSString*)week:(NSInteger)week{    NSString*weekStr=nil;    if(week==1)    {        [email protected]"星期天";    }else if(week==2){        [email protected]"星期一";    }else if(week==3){        [email protected]"星期二";    }else if(week==4){        [email protected]"星期三";    }else if(week==5){        [email protected]"星期四";    }else if(week==6){        [email protected]"星期五";    }else if(week==7){        [email protected]"星期六";    }    return weekStr;}

由NSDate 轉換為NSString:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];NSLog(@"%@", strDate);


由NSString 轉換為NSDate:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];NSLog(@"%@", date);


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.