Additional setup after loading the view, typically from a nib. Get the current date note week1 is Sunday nsdate *date = [NSDate Date]; NSLog (@ "date:%@", date); Get (24 * 60 * 60) that is 24 hours before the date, 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 is Sunday, Week7 is Saturday; 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); Get millisecond 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] "Sunday"; } else if (week==2) { [email protected] "Monday"; } else if (week==3) { [email protected] "Tuesday"; } else if (week==4) { [email protected] "Wednesday"; } else if (week==5) { [email protected] "Thursday"; } else if (week==6) { [email protected] "Friday"; } else if (week==7) { [email protected] "Saturday"; } return WEEKSTR;}
byNSDateconverted toNSString:
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"]; NSString *strdate = [dateformatter stringfromdate:[nsdate Date]]; NSLog (@ "%@", strdate);
by NSString conversion to 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);