Object C 獲取日期

2021-05-24 10:04:26 字數 2127 閱讀 7807

view plaincopy to clipboardprint?

//得到當前的日期

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] autorelease];

nsdate *date = [nsdate date];

[formatter settimestyle:nsdateformattermediumstyle];

nscalendar *calendar = [[[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar] autorelease];

nsdatecomponents *comps = [[[nsdatecomponents alloc] init] autorelease];

nsinteger unitflags = nsyearcalendarunit |

nsmonthcalendarunit |

nsdaycalendarunit |

nsweekdaycalendarunit |

nshourcalendarunit |

nsminutecalendarunit |

nssecondcalendarunit;

//int week=0;

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);

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]]);

[dateformatter release];

杜思波 2011 湖南

python 獲取日期 python 獲取日期

python 獲取日期我們需要用到time模組,比如time.strftime方法 time.strftime y m d time.localtime time.time 最後用time.strftime 方法,把剛才的一大串資訊格式化成我們想要的東西,現在的結果是 2013 03 31 time...

名稱日期python 獲取日期

python 取獲日期 我們要需用到time模組,比如time.strftime法方 time.strftime y m d time.localtime time.time 最後用time.strftime 法方,把剛才的一大串資訊格式化成我們想要的貨色,在現的結果是 2013 03 31 tim...

sql獲取日期

declare aa varchar 20 set aa convert varchar 100 getdate 23 日 print aa 0 00 00.000 print aa 23 59 59.999 周 print convert varchar 100 dateadd wk,datedi...