简体   繁体   中英

HOW to set GMT of date picker ,date picker show wrong time

While i am setting up time Zome ,locale time zone still datepicker returning wrong time .

datepicker.timeZone = [NSTimeZone localTimeZone];

It should display like 2012-04-20 12:20 but it is displaying 2012-04-20 12:02:42 + 0000

How can i set Time Zone of India to datepicker ?

i think you should set your dateformatter's Locale like below:

-(void)localDate:(NSDate*)date
{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; // set your formatter how you want
    NSDate * t = [dateFormatter dateFromString:[date description]];

    //[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; //you can set date format style
    NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"yourLocalidentifier"] autorelease]; 
    //you can reach your local identifier from here: [NSLocale availableLocaleIdentifiers]

    [dateFormatter setLocale:usLocale];
    NSLog(@"%@",[dateFormatter stringFromDate:t]); 
}

For indian timezone in datepicker

Objective C

NSTimeInterval seconds = 5 * 60 * 60 + 30;
datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:seconds]; // Like GMT+5:30

in Swift 3

let seconds: TimeInterval = 5*60*60 + 30
datePicker.timeZone = TimeZone(secondsFromGMT: Int(seconds))  // Like GMT+5:30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM