मैं NSDateउद्देश्य C या / और स्विफ्ट में UTC को स्थानीय समयक्षेत्र NSDate में कैसे परिवर्तित करूं ?
मैं NSDateउद्देश्य C या / और स्विफ्ट में UTC को स्थानीय समयक्षेत्र NSDate में कैसे परिवर्तित करूं ?
जवाबों:
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[df_utc setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
[df_local setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
// you can also use NSDateFormatter dateFromString to go the opposite way
स्वरूपण स्ट्रिंग मापदंडों की तालिका:
https://waracle.com/iphone-nsdateformatter-date-formatting-table/
यदि प्रदर्शन एक प्राथमिकता है, तो आप उपयोग करने पर विचार कर सकते हैं strftime
NSDateवस्तु में कोई रूपांतरण आवश्यक नहीं है, जो सही है। एक समयक्षेत्र में रूपांतरण तब होता है जब दिनांक स्वरूपित होती है, न कि जब इसे बनाया जाता है, क्योंकि तिथियों में समय-क्षेत्र नहीं होता है।
NSCalendarDateपदावनत किया जाता है।
संपादित करें जब मैंने यह लिखा था तो मुझे नहीं पता था कि मुझे एक डेटफॉर्मेटर का उपयोग करना चाहिए जो शायद एक बेहतर दृष्टिकोण है, इसलिए slfइसका उत्तर भी देखें।
मेरे पास एक webservice है जो UTC में दिनांक लौटाता है। मैं toLocalTimeइसे स्थानीय समय toGlobalTimeमें परिवर्तित करने और यदि आवश्यक हो तो वापस परिवर्तित करने के लिए उपयोग करता हूं ।
यहीं से मुझे अपना उत्तर मिला:
https://agilewarrior.wordpress.com/2012/06/27/how-to-convert-nsdate-to-different-time-zones/
@implementation NSDate(Utils)
-(NSDate *) toLocalTime
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: self];
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
}
-(NSDate *) toGlobalTime
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = -[tz secondsFromGMTForDate: self];
return [NSDate dateWithTimeInterval: seconds sinceDate: self];
}
@end
सबसे आसान तरीका जो मैंने पाया है वह यह है:
NSDate *someDateInUTC = …;
NSTimeInterval timeZoneSeconds = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate *dateInLocalTimezone = [someDateInUTC dateByAddingTimeInterval:timeZoneSeconds];
secondsFromGMTForDateयदि आप दिन के उजाले की बचत के लिए खाते का उपयोग करना चाहते हैं, तो इसके अलावा, का उपयोग किया जाना चाहिए। देखें एप्पल डॉक्स
स्विफ्ट 3+ : यूटीसी लोकल और लोकल से यूटीसी
extension Date {
// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
let timezone = TimeZone.current
let seconds = TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
}
यदि आप स्थानीय दिनांक और समय चाहते हैं। इस कोड को आज़माएं: -
NSString *localDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
[NSDate date]साथ प्रतिस्थापित करना होगा [NSDate dateWithNaturalLanguageString:sMyDateString]।
अपनी UTC तिथि को स्थानीय दिनांक में बदलें
-(NSString *)getLocalDateTimeFromUTC:(NSString *)strDate
{
NSDateFormatter *dtFormat = [[NSDateFormatter alloc] init];
[dtFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dtFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *aDate = [dtFormat dateFromString:strDate];
[dtFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dtFormat setTimeZone:[NSTimeZone systemTimeZone]];
return [dtFormat stringFromDate:aDate];
}
इस तरह का उपयोग करें
NSString *localDate = [self getLocalDateTimeFromUTC:@"yourUTCDate"];
यहाँ इनपुट स्ट्रिंग स्ट्रिंग है (प्रारूप 08/30/2012 11:11 में) GMT में इनपुट समय को सिस्टम सेट ज़ोन समय में परिवर्तित करता है
//UTC time
NSDateFormatter *utcDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[utcDateFormatter setDateFormat:@"MM/dd/yyyy HH:mm"];
[utcDateFormatter setTimeZone :[NSTimeZone timeZoneForSecondsFromGMT: 0]];
// utc format
NSDate *dateInUTC = [utcDateFormatter dateFromString: currentUTCTime];
// offset second
NSInteger seconds = [[NSTimeZone systemTimeZone] secondsFromGMT];
// format it and send
NSDateFormatter *localDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[localDateFormatter setDateFormat:@"MM/dd/yyyy HH:mm"];
[localDateFormatter setTimeZone :[NSTimeZone timeZoneForSecondsFromGMT: seconds]];
// formatted string
NSString *localDate = [localDateFormatter stringFromDate: dateInUTC];
return localDate;
//This is basic way to get time of any GMT time.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"]; // 09:30 AM
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:1]]; // For GMT+1
NSString *time = [formatter stringFromDate:[NSDate date]]; // Current time
उपयुक्त स्थानीय NSTimeZone के साथ UTC कैलेंडर से दिनांक को एक में परिवर्तित करें ।
मैं इस विधि को हमारे LocalTimeZone में दिनांक समय बदलने के लिए लिखता हूं
-हेरे (NSString *) टाइमजोन पैरामीटर एक सर्वर टाइमज़ोन है
-(NSString *)convertTimeIntoLocal:(NSString *)defaultTime :(NSString *)TimeZone
{
NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
[serverFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:TimeZone]];
[serverFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *theDate = [serverFormatter dateFromString:defaultTime];
NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[userFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *dateConverted = [userFormatter stringFromDate:theDate];
return dateConverted;
}
चूंकि कोई भी उपयोग नहीं कर NSDateComponentsरहा था, मुझे लगा कि मैं इसमें एक पिच करूंगा ... इस संस्करण में, कोई NSDateFormatterउपयोग नहीं किया जाता है, इसलिए कोई स्ट्रिंग पार्सिंग NSDateनहीं है , और जीएमटी (यूटीसी) के बाहर समय का प्रतिनिधित्व करने के लिए उपयोग नहीं किया जाता है। मूल NSDateचर में है i_date।
NSCalendar *anotherCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:i_anotherCalendar];
anotherCalendar.timeZone = [NSTimeZone timeZoneWithName:i_anotherTimeZone];
NSDateComponents *anotherComponents = [anotherCalendar components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitNanosecond) fromDate:i_date];
// The following is just for checking
anotherComponents.calendar = anotherCalendar; // anotherComponents.date is nil without this
NSDate *anotherDate = anotherComponents.date;
i_anotherCalendarNSCalendarIdentifierGregorianया कोई अन्य कैलेंडर हो सकता है । के लिए NSStringअनुमति के i_anotherTimeZoneसाथ हासिल किया जा सकता है [NSTimeZone knownTimeZoneNames], लेकिन या या anotherCalendar.timeZoneहो सकता है[NSTimeZone defaultTimeZone][NSTimeZone localTimeZone][NSTimeZone systemTimeZone] पूरी तरह।
यह वास्तव anotherComponentsमें नए समय क्षेत्र में समय धारण कर रहा है। आप देखेंगे कि यह जीएमटी (यूटीसी) में समय के anotherDateबराबर है i_date।
आप इसे आज़मा सकते हैं:
NSDate *currentDate = [[NSDate alloc] init];
NSTimeZone *timeZone = [NSTimeZone defaultTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"ZZZ"];
NSString *localDateString = [dateFormatter stringFromDate:currentDate];
NSMutableString *mu = [NSMutableString stringWithString:localDateString];
[mu insertString:@":" atIndex:3];
NSString *strTimeZone = [NSString stringWithFormat:@"(GMT%@)%@",mu,timeZone.name];
NSLog(@"%@",strTimeZone);
यूटीसी समय को वर्तमान समय क्षेत्र में परिवर्तित करें।
कॉल फ़ंक्शन
NSLocale *locale = [NSLocale autoupdatingCurrentLocale];
NSString *myLanguageCode = [locale objectForKey: NSLocaleLanguageCode];
NSString *myCountryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *rfc3339DateTimeString = @"2015-02-15 00:00:00"];
NSDate *myDateTime = (NSDate*)[_myCommonFunctions _ConvertUTCTimeToLocalTimeWithFormat:rfc3339DateTimeString LanguageCode:myLanguageCode CountryCode:myCountryCode Formated:NO];
समारोह
-NSObject*)_ConvertUTCTimeToLocalTimeWithFormat:rfc3339DateTimeString LanguageCode:(NSString *)lgc CountryCode:(NSString *)ctc Formated:(BOOL) formated
{
NSDateFormatter *sUserVisibleDateFormatter = nil;
NSDateFormatter *sRFC3339DateFormatter = nil;
NSTimeZone *timeZone = [NSTimeZone defaultTimeZone];
if (sRFC3339DateFormatter == nil)
{
sRFC3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *myPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:[NSString stringWithFormat:@"%@", timeZone]];
[sRFC3339DateFormatter setLocale:myPOSIXLocale];
[sRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[sRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
}
// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [sRFC3339DateFormatter dateFromString:rfc3339DateTimeString];
if (formated == YES)
{
NSString *userVisibleDateTimeString;
if (date != nil)
{
if (sUserVisibleDateFormatter == nil)
{
sUserVisibleDateFormatter = [[NSDateFormatter alloc] init];
[sUserVisibleDateFormatter setDateStyle:NSDateFormatterMediumStyle];
[sUserVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
}
// Convert the date object to a user-visible date string.
userVisibleDateTimeString = [sUserVisibleDateFormatter stringFromDate:date];
return (NSObject*)userVisibleDateTimeString;
}
}
return (NSObject*)date;
}