ios - How to create NSDate like '2013-05-11 00:00:00' -
this question has answer here:
this basic question, guess, can't seem find answer in of books.
suppose have date, created this:
nsdate *today = [nsdate date];
but want change time part of 'today' '00:00:00'. how can this?
---- added ----
i tried this:
nsdatecomponents *components = [[nsdatecomponents alloc] init]; [components setday:11]; [components setmonth:5]; [components setyear:2013]; [components sethour:0]; [components setminute:0]; [components setsecond:0]; nscalendar *calendar = [nscalendar currentcalendar]; nsdate *startdate = [calendar datefromcomponents:components]; nslog(@"current date: %@", startdate);
but nslog prints out this:
current date: 2013-05-11 04:00:00 +0000
i'm guessing has ny timezone?
-- answer --
here answer looking it. trick eliminate 04:00:00
time set timezone gmt:
[calendar settimezone:[nstimezone timezoneforsecondsfromgmt:0]];
-- revised answer --
but might want local time -- example, if need find record in core data date, , stored date in local time -- might not want set timezone gmt, after all.
if want set components of date, want @ nscalendar's methods dealing date components:
nsdate *today = [nsdate date]; // year, month, day date nsdatecomponents *components = [[nscalendar currentcalendar] components:nscalendarunityear | nscalendarunitmonth | nscalendarunitday fromdate:today]; // set hour, minute, second 0 components.hour = 0; components.minute = 0; components.second = 0; // create date nsdate *date = [[nscalendar currentcalendar] datefromcomponents:components];
Comments
Post a Comment