NSDate: discharge time only

I use the code below to retrieve a date only from NSDate. What am I doing wrong?

NSDate* now = [NSDate date]; NSLog(@"Now Date %@",now); NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"dd-MM-yyyy"; NSString *stringDate = [format stringFromDate:now]; NSDate *todaysDate = [format dateFromString:stringDate]; NSLog(@"Today Date without Time %@", todaysDate); 

Log:

 2014-06-21 12:27:23.284 App[69727:f03] Now Date 2014-06-21 19:27:23 +0000 2014-06-21 12:27:23.285 App[69727:f03] Today Date without Time 2014-06-21 07:00:00 +0000 

Why do I get: 07:00:00 +0000 at the end?

I would like to get NSDate in the following format:

 2014-06-21 00:00:00 +0000 

Availability 0 for time, seconds, etc. it does not matter.

+6
source share
5 answers
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSDate *now = [[NSDate alloc] init]; NSString *theDate = [dateFormat stringFromDate:now]; 

must work

+4
source

Another solution: using NSCalendar:

 NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:1]]; // I'm in Paris (+1) NSDateComponents *comps = [cal components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]]; comps.hour = 0; comps.minute = 0; comps.second = 0; NSDate *newDate = [cal dateFromComponents:comps ]; NSLog(@"date: %@",newDate); 

Adjust the time zone parameter, you will get something like: date: 2014-06-21 00:00:00 +0000

+5
source

If you do not like the time, NSDate is not suitable for you. NSDate represents a specific point in time - NSDate is absent without time. What you see is a registered NSDate description, which is a full listing in GMT.

If you want to track only the year, month and day, use NSDateComponents and retrieve only those components that interest you. Then you can use the component object and pass it on your own.

+3
source
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSString *strDate = [dateFormatter stringFromDate:[NSDate date]]; 

Using the code above, the NSDate object will have time. But the string will only be date text.

+2
source

This is code using.

 #include <time.h> - (NSDate *)dateFromISO8601String:(NSString *)string { if (!string) { return nil; } struct tm tm; time_t t; strptime([string cStringUsingEncoding:NSUTF8StringEncoding], "%Y-%m-%dT%H:%M:%S%z", &tm); tm.tm_hour = 0; tm.tm_min = 0; tm.tm_sec = 0; tm.tm_isdst = -1; t = mktime(&tm); return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]]; } - (NSString *)ISO8601String:(NSDate*)aDate { struct tm *timeinfo; char buffer[80]; time_t rawtime = [aDate timeIntervalSince1970] - [[NSTimeZone localTimeZone] secondsFromGMT]; timeinfo = localtime(&rawtime); strftime(buffer, 80, "%Y-%m-%dT%H:%M:%S%z", timeinfo); return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding]; } NSString *currentDateStr = [self ISO8601String:[NSDate date]]; NSDate *dateWithoutTime = [self dateFromISO8601String:currentDateStr]; NSLog(@"currentDateStr: %@",currentDateStr); NSLog(@"dateWithoutTime: %@",dateWithoutTime); 
0
source

Source: https://habr.com/ru/post/971153/


All Articles