การจัดรูปแบบวันที่จากเวลาเซิร์ฟเวอร์เป็นเวลาท้องถิ่นโดยใช้ NSTimeZone และ Dateformatter

ฉันได้รับวันที่/เวลาเป็น 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

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมอย่างมาก


person HurkNburkS    schedule 22.08.2013    source แหล่งที่มา
comment
วันที่เซิร์ฟเวอร์ของคุณแจ้งเขตเวลาใด คุณต้องตั้งค่าเขตเวลาของ NSDateFormatter แรกเป็นโซนเวลาของเซิร์ฟเวอร์ เพื่อให้ NSDateFormatter ตีความสตริงในเขตเวลาของเซิร์ฟเวอร์ ด้วยวิธีนี้ เมื่อคุณได้รับผลลัพธ์ NSDate มันจะแสดงถึงจุดเวลาที่ถูกต้อง (โปรดทราบว่าออบเจ็กต์ NSDate จะไม่คำนึงถึงเขตเวลา โดยจะแสดงถึงช่วงเวลาที่เฉพาะเจาะจงโดยไม่คำนึงถึงเขตเวลาและการออมแสง ฯลฯ) เมื่อคุณแยกวิเคราะห์ช่วงเวลาที่เจาะจงอย่างถูกต้อง คุณสามารถแสดงช่วงเวลานั้นในเวลาท้องถิ่นได้โดยใช้ตัวจัดรูปแบบวันที่ที่สอง   -  person dreamlax    schedule 22.08.2013
comment
โอเค.. ดังนั้นเขตเวลาที่เซิร์ฟเวอร์ของฉันตั้งไว้เป็น UTC   -  person HurkNburkS    schedule 22.08.2013
comment
นั่นได้ผล ฉันตั้งค่า dateFormatter เป็น UTC จากนั้น dateFormatter1 ตั้งค่าเป็น timeZone.. ทำงานได้อย่างสมบูรณ์แบบ :) ฉันแค่ต้องเปลี่ยนชื่อ var เพื่อแสดงข้อมูลของฉันให้ดีขึ้นเล็กน้อย :) ขอบคุณสำหรับความช่วยเหลือ   -  person HurkNburkS    schedule 22.08.2013
comment
วัตถุ NSDate มี (หรือควรมี ถ้าสร้างอย่างถูกต้อง) ค่า GMT/UTC ของวันที่/เวลา การหาเวลาในเขตเวลาท้องถิ่นของคุณเป็นหน้าที่ของตัวจัดรูปแบบวันที่ คุณไม่แก้ไขวัตถุ NSDate เพื่อตั้งค่าเขตเวลา   -  person Hot Licks    schedule 22.08.2013


คำตอบ (1)


NSDateFormatter ที่อ่านวันที่จะต้องตั้งค่าเป็นเขตเวลาที่คุณกำลังแยกวิเคราะห์ ในกรณีของคุณคือ UTC ตัวจัดรูปแบบวันที่จะสามารถสร้างวัตถุ NSDate (ซึ่งแสดงถึงช่วงเวลาที่เฉพาะเจาะจงโดยไม่คำนึงถึงเขตเวลา) จากนั้นคุณสามารถมอบออบเจ็กต์ NSDate นั้นให้กับ NSDateFormatter อื่นที่ได้รับการกำหนดค่าให้จัดรูปแบบวันที่ในเขตเวลาที่ระบุ

// set date format
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = @"yyyy-MM-dd HH:mm:ss";
dateParser.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *specificMomentInTime = [dateParser dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:@"ActivationTime"]];

// reformat converted Time to readable format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM/yy - hh:mm a";
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:specificMomentInTime];

NSLog(@"UTC ServerTime - %@", specificMomentInTime);
NSLog(@"NewFormat - %@", dateWithNewFormat);
person dreamlax    schedule 22.08.2013