How can I output the current date and time with the default timezone? Whenever I use the following code, I always get time in GMT. Is it possible to convert NSDate into default timezone? I can not use NSDateFormatter because I need an NSDate object with the right time.
NSDate * date = [NSDate date]; NSLog (@ "time:% @", date);
Output: 2010-09-26 12:07:12 TimeTest [2641: 207] Time: 2010-09-26 12:07:12 GMT
< Strong> Update: How do I use NSDateFormatter with SetTimeZone when I have to return an NSDate object with the right time?
NSDate * date = [NSDate date]; NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease]; [Format setDateFormat: @ "yyyy-MM-dd hh: mm: ss"]; NSTimeZone * tz = [NSTimeZone timezone name: @ "megs"]; [Setter set time zone: TJ]; NSString * datestr = [Formatter string stringed: date]; NSLog (@ "% @", DataStar); NSDT * Datanev = [Formatter Debt Frostring: String]; NSLog (@ "% @", Daytonu);
The first NSLG produces the right time, but when I want to create an NSDate object with the right time, I get the wrong time.
It does not make sense NSDate
represents a point in time, it does not know or care about time zone, for example Today, 12:00 GMT and today, 06:00 -0600 will be represented by another same NSDate
object.
To display a date in a specific time zone, use NSDateFormatter
, specifically its setTimeZone:
method.
Comments
Post a Comment