Current Date and Time - NSDate

I need to display the current date and time.

I used;

NSDate *currentDateNTime = [NSDate date]; 

I want to have the current date and time (the system time should be displayed, not Greenwich Mean Time). The output should be in NSDate format, not NSString .

eg:

  NSDate *currentDateNTime = [NSDate date]; // Do the processing.... NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString 
+4
source share
5 answers

Since all NSDate is GMT, you probably want to: (do not forget that nowDate will not be the actual current system date-time, but will "shift", so if you create an NSString using NSDateFormatter, you will see the wrong date)

 NSDate* currentDate = [NSDate date]; NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone]; NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate]; NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate]; NSTimeInterval interval = nowGMTOffset - currentGMTOffset; NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate]; 
+4
source

Each point in time - this is the same point in time throughout the world - it simply expresses itself as a different clock in different time zones. Therefore, you cannot change the date to another date that represents the time in your time zone; you must use the NSDateFormatter that you use with the time zone you are in. The resulting string is a point in time expressed in the hourly time of your position.

Do all the necessary calculations in GMT and just use the formatter to display.

+5
source

You will need an NSDateFormatter and call the stringFromDate method to get your date string.

 NSDateFormatter *dateformater = [[NSDateFormatter alloc] init]; [dateformater setDateFormat:@"yyyyMMdd,HH:mm"]; NSString *str = [dateformater stringFromDate: currentDateNTime]; 
0
source

Worth reading Date [NSDate date] returns local date and time?

Some useful resources for those who come to this recently:

Apple's Date and Time Programming Guide reads it if you are doing something serious with dates and times. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i?language=objc

A useful category in NSDate with many utilities allows you to create a new date based on an existing date. https://github.com/erica/NSDate-Extensions

There is also a quick version of the category https://github.com/erica/SwiftDates

0
source

use this method

 -(NSDate *)convertDateToDate:(NSDate *) date { NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; NSDate *nowDate = [[[NSDate alloc] init] autorelease]; [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [formatter setDateFormat:@"yyyy-MM-d H:m:s"]; NSString * strdate = [formatter stringFromDate:date]; nowDate = [formatter dateFromString:strdate]; return nowDate; } 

it can return you what you want.

Hope this helps you.

-1
source

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


All Articles