ฉันได้รับวันที่/เวลาเป็น NSString จากเซิร์ฟเวอร์ของฉัน โดยที่ฉันกำลังแปลงเวลานั้นเป็น NSDate เป็นเวลาท้องถิ่นของผู้ใช้โดยใช้ NSTimeZone หลังจากนั้นฉันพยายามฟอร์แมต NSDate นี้ใหม่ให้เป็น NSString ที่สามารถอ่านได้ดีขึ้นโดยใช้รูปแบบ NSDateFormatter ใหม่ แต่เมื่อฉันพยายามใช้รูปแบบใหม่นี้ มันจะเปลี่ยน dateString ที่เป็นผลลัพธ์กลับเป็นเวลาเซิร์ฟเวอร์ดั้งเดิม
ฉันอยากจะรู้ว่าฉันทำอะไรผิด ฉันอยากจะแสดงเวลาที่แปลงแล้วในรูปแบบใหม่
นี่คือรหัสที่ฉันใช้
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// change time to systemTimeZone
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSDate *localTime = [dateFormatter dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:@"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [dateFormat1 stringFromDate:localTime];
NSLog(@"TimeZone - %@", timeZone);
NSLog(@"UTC ServerTime - %@", [singleInstanceActivationHistoryDictionay objectForKey:@"ActivationTime"]);
NSLog(@"UTC to deviceTimeZone - %@", localTime);
NSLog(@"NewFormat - %@", dateWithNewFormat);
นี่คือตัวอย่างผลลัพธ์ของฉัน
TimeZone - Pacific/Auckland (NZST) offset 43200
UTC ServerTime - 2013-08-22 01:45:59
UTC to deviceTimeZone - 2013-08-21 13:45:59 +0000
NewFormat - 22/08/13 - 01:45 AM
ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมอย่างมาก
NSDate
มันจะแสดงถึงจุดเวลาที่ถูกต้อง (โปรดทราบว่าออบเจ็กต์NSDate
จะไม่คำนึงถึงเขตเวลา โดยจะแสดงถึงช่วงเวลาที่เฉพาะเจาะจงโดยไม่คำนึงถึงเขตเวลาและการออมแสง ฯลฯ) เมื่อคุณแยกวิเคราะห์ช่วงเวลาที่เจาะจงอย่างถูกต้อง คุณสามารถแสดงช่วงเวลานั้นในเวลาท้องถิ่นได้โดยใช้ตัวจัดรูปแบบวันที่ที่สอง - person dreamlax   schedule 22.08.2013