8

I need to display the current Date and Time.

I have used ;

NSDate *currentDateNTime        = [NSDate date];

I want to have the current date and time (Should display the system time and not GMT time). The output should be in a NSDate format and not NSString.

for example;

    NSDate *currentDateNTime        = [NSDate date];
// Do the processing....

NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140
  • 4
    What do you mean? `[NSDate date]` will give you the current time. What do you want? – mattjgalloway May 28 '12 at 16:30
  • There is no question here. [NSDate date] returns current date! – Garoal May 28 '12 at 16:35
  • I want to get both current Date and TIme. ex : 2012 May 23 04.50 PM – Sharon Watinsan May 28 '12 at 16:47
  • `[NSDate date]` returns current date and time. – mattjgalloway May 28 '12 at 16:48
  • 3
    I know that. but i need it to return the System time and not GMT time. – Sharon Watinsan May 28 '12 at 16:49
  • NSDate has a property timeZone, you date is GMT time plus system timeZone. – Giorgio Marziani de Paolis May 28 '12 at 16:51
  • 1
    duplicate of http://stackoverflow.com/questions/10772033/get-current-date-time-with-nsdate-date/10772052#10772052 – Parag Bafna May 28 '12 at 17:18
  • @netrace: `NSDate` has no such property. – jscs May 28 '12 at 17:27
  • As asked, the question is nonsensical. The request is for an `NSDate` *that is in the current system time zone*. There is no such concept; `NSDate` is ***always*** in UTC. While the accepted answer, in some sense, does what is requested, doing so ***is a terrible idea***. Don't ever do this - the result is an entity that claims to represent one datetime, yet actually represents a different one. I don't know iOS well enough to know whether it has since added a class that carries both a datetime and a timezone; if not you should make your own, and pass that around. – ToolmakerSteve Aug 27 '16 at 17:46

5 Answers5

11

Since all NSDate is GMT referred, you probably want this: (don'f forget that the nowDate won't be the actual current system date-time, but it's "shifted", so if you will generate NSString using NSDateFormatter, you will see a 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];
  • So, how can display the current date and current time (Not he GMT time, but the System time) – Sharon Watinsan May 28 '12 at 16:49
  • if you need to display, you need NSString. How do you display the NSDate object? – Giorgio Marziani de Paolis May 28 '12 at 16:50
  • I am not going to display, but use the NSDate to do further calculation of the current date and time. – Sharon Watinsan May 28 '12 at 16:54
  • 2
    This is _terrible_ advice. You shouldn't be manually shifting dates to compensate for time zones. Use `NSDateFormatter` to display a date in a particular zone. – jscs May 28 '12 at 18:56
  • @JacquesCousteau it's not an advice, it's what sharon need. – Giorgio Marziani de Paolis May 29 '12 at 08:08
  • this seems to me to be a quite convenient method to screw up the whole date and time handling and all calendrical calculations, as you are jumping in time instead of switching the time zone. In the long run it will be the root for many problems — for sure. – vikingosegundo Jul 12 '12 at 15:42
  • 1
    I'm sorry, but even though this is what was requested, I downvote this answer. Attempt to talk the questioner out of this approach, or at least *WARN* other readers they don't want to do this. To pass around a datetime that is not what it appears to be is fundamentally horrible design. One solution is to pass around a class or structure with two fields (NSDate, NSTimeZone). If you have to interact with legacy code that can't be adapted, then yes you might need a converter like this. Restrict use of the "lying" NSDate to a small part of your code; use the "timezoned" structure elsewhere. – ToolmakerSteve Aug 27 '16 at 17:56
  • Warned enough, it's very clear that nsdate instance contains a wrong date. – Giorgio Marziani de Paolis Aug 27 '16 at 18:50
  • But please, help me to warn better than what I did. – Giorgio Marziani de Paolis Aug 27 '16 at 18:52
  • @ToolmakerSteve, did you already edited the answer to warn better than what I did? – Giorgio Marziani de Paolis Sep 21 '16 at 15:50
5

Every moment in time is the same moment in time everywhere around the world —- it is just expressed as different clock times in different timezones. Therefore, you can't change the date to some other date that represents the time in your timezone; you must use an NSDateFormatter that you feed with the timezone you are in. The resulting string is the moment in time expressed in the clock time of your position.

Do all needed calculations in GMT, and just use a formatter for displaying.

jscs
  • 63,694
  • 13
  • 151
  • 195
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Exactly. A shame that no one put comment on the original question, asking OP *why* they needed to do what they said they needed to do - since what they requested *is a terrible design*, given the definition of NSDate as timezone-independent. To get enough information to suggest an alternative approach -- or at least to "contain the damage" (limit it to a small area of code; carry timezone around elsewhere if necessary, so can format as desired). – ToolmakerSteve Aug 27 '16 at 18:02
1

Worth reading Does [NSDate date] return the local date and time?

Some useful resources for anyone coming to this more recently:

Apple date and time programming guide do read it if you're doing anything serious with dates and times. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i?language=objc

Useful category on NSDate with lots of utilities does allow a ~new~ date to be generated based on an existing date. https://github.com/erica/NSDate-Extensions

There's also a swift version of the category https://github.com/erica/SwiftDates

ReaddyEddy
  • 323
  • 4
  • 7
0

You need an NSDateFormatter and call stringFromDate this method to get a string of your date.

NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformater setDateFormat:@"yyyyMMdd,HH:mm"];
NSString *str = [dateformater stringFromDate: currentDateNTime];
Asdrubal
  • 2,421
  • 4
  • 29
  • 37
lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • I don't want to output the result in String. It should be NSDate instead. and when i try to convert it to NSDate (with `dateFromString`) it will show me the GMT time and not the system time. – Sharon Watinsan May 28 '12 at 16:48
-1

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

this may return you what you want.

i hope you this may help you.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Nilesh Kikani
  • 2,628
  • 21
  • 37
  • this seems to me to be a quite convenient method to screw up the whole date and time handling and all calendrical calculations, as you are jumping in time instead of switching the time zone. – vikingosegundo Jul 12 '12 at 15:41
  • I doubt this is correct, since there appears to be no mention of the *system time* (current locale), which is what the question asks for. (If this somehow does produce the desired output, how it does is non-intuitive.) – ToolmakerSteve Aug 27 '16 at 17:37