• <small id='rXPU2'></small><noframes id='rXPU2'>

    1. <i id='rXPU2'><tr id='rXPU2'><dt id='rXPU2'><q id='rXPU2'><span id='rXPU2'><b id='rXPU2'><form id='rXPU2'><ins id='rXPU2'></ins><ul id='rXPU2'></ul><sub id='rXPU2'></sub></form><legend id='rXPU2'></legend><bdo id='rXPU2'><pre id='rXPU2'><center id='rXPU2'></center></pre></bdo></b><th id='rXPU2'></th></span></q></dt></tr></i><div id='rXPU2'><tfoot id='rXPU2'></tfoot><dl id='rXPU2'><fieldset id='rXPU2'></fieldset></dl></div>

      <tfoot id='rXPU2'></tfoot>
      • <bdo id='rXPU2'></bdo><ul id='rXPU2'></ul>
        <legend id='rXPU2'><style id='rXPU2'><dir id='rXPU2'><q id='rXPU2'></q></dir></style></legend>

        为什么我的时区没有保存到我的 NSDate 中?

        时间:2023-05-19
          <bdo id='Hg53Q'></bdo><ul id='Hg53Q'></ul>

          1. <legend id='Hg53Q'><style id='Hg53Q'><dir id='Hg53Q'><q id='Hg53Q'></q></dir></style></legend>
          2. <i id='Hg53Q'><tr id='Hg53Q'><dt id='Hg53Q'><q id='Hg53Q'><span id='Hg53Q'><b id='Hg53Q'><form id='Hg53Q'><ins id='Hg53Q'></ins><ul id='Hg53Q'></ul><sub id='Hg53Q'></sub></form><legend id='Hg53Q'></legend><bdo id='Hg53Q'><pre id='Hg53Q'><center id='Hg53Q'></center></pre></bdo></b><th id='Hg53Q'></th></span></q></dt></tr></i><div id='Hg53Q'><tfoot id='Hg53Q'></tfoot><dl id='Hg53Q'><fieldset id='Hg53Q'></fieldset></dl></div>

            1. <small id='Hg53Q'></small><noframes id='Hg53Q'>

                  <tfoot id='Hg53Q'></tfoot>
                    <tbody id='Hg53Q'></tbody>
                  本文介绍了为什么我的时区没有保存到我的 NSDate 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我必须在objective-c 中从NSString 初始化一个NSDate 对象.我是这样做的:

                  I must initialize an NSDate object from NSString in objective-c. I do it like this:

                  NSString *dateString = [[webSentence child:@"DateTime"].text stringByReplacingOccurrencesOfString:@"T" withString:@" "];
                  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                  [dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
                  [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Budapest"]];
                  
                  NSDate *date = [[NSDate alloc] init];
                  date = [dateFormatter dateFromString:dateString];
                  

                  例如:当我尝试使用字符串值 @"2011-01-02 17:49:54" 时,我得到一个 NSDate 2011-01-02 16:49:54 +0000.如您所见,这两个值之间存在一小时的差异.NSDate 的值错误,它应该与我在 dateFormatter 中设置的时区中的字符串中定义的完全相同.即使我将其时区设置为欧洲/布达佩斯",它似乎也使用我将其定义为 UTC 的日期字符串.我该如何解决这个问题?

                  E.g: when I try it with string value @"2011-01-02 17:49:54" I get an NSDate 2011-01-02 16:49:54 +0000. As you can see there is a one hour difference between the two values. NSDate has a wrong value, it should be exactly the same I defined in my string in the timezone I set in dateFormatter. It seems it uses my date defined it string as UTC, even if I set its timezone to "Europe/Budapest". How can I fix this problem?

                  谢谢!

                  推荐答案

                  两件事:

                  1) 您的日期格式字符串有错误.您应该使用 MM 表示月份,而不是 mm(小写 mm 表示分钟)

                  1) you have an error in your date format string. You should use MM for month, not mm (lowercase mm is for minutes)

                  2) 创建 NSDate 对象后,您需要使用 NSDateFormatter 方法 stringFromDate: 生成本地化到特定时区的日期字符串.如果您只是在 NSDate 对象上直接执行 NSLog(),它将默认将日期显示为 GMT(GMT 比布达佩斯时间晚一小时)

                  2) after you create you NSDate object, you'll need to use the NSDateFormatter method stringFromDate: to generate a date string localized to a particular timezone. If you just do a straight NSLog() on the NSDate object it will show the date as GMT by default (GMT is one hour behind Budapest time)

                  这篇关于为什么我的时区没有保存到我的 NSDate 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从 iPhone 获取时区/国家 下一篇:iPhone的日期和时间在哪里?获取时区列表的时间

                  相关文章

                    <bdo id='YPg5d'></bdo><ul id='YPg5d'></ul>

                    <small id='YPg5d'></small><noframes id='YPg5d'>

                    1. <legend id='YPg5d'><style id='YPg5d'><dir id='YPg5d'><q id='YPg5d'></q></dir></style></legend><tfoot id='YPg5d'></tfoot>
                      <i id='YPg5d'><tr id='YPg5d'><dt id='YPg5d'><q id='YPg5d'><span id='YPg5d'><b id='YPg5d'><form id='YPg5d'><ins id='YPg5d'></ins><ul id='YPg5d'></ul><sub id='YPg5d'></sub></form><legend id='YPg5d'></legend><bdo id='YPg5d'><pre id='YPg5d'><center id='YPg5d'></center></pre></bdo></b><th id='YPg5d'></th></span></q></dt></tr></i><div id='YPg5d'><tfoot id='YPg5d'></tfoot><dl id='YPg5d'><fieldset id='YPg5d'></fieldset></dl></div>