简体   繁体   English

DateFormatter在iOS中不起作用通过设置

[英]DateFormatter not working in ios By setting

I am using date formatter in xcode 6.2 with swift.It is working fine in the ios 8.1 but when I am testing my app in ios above 8.1(I tried in 8.2 and 8.4 ) the date formatter is not working. 我在swift的xcode 6.2中使用日期格式化程序。它在ios 8.1中工作正常,但是当我在8.1以上的ios中测试我的应用程序时(我在8.2和8.4中尝试过),日期格式化程序不起作用。 Does any one faces similar problem. 是否有人面临类似的问题。 This is the type of date I am getting In string format 10-08-2015T13:59:53+0000 . 这是我要获取的日期类型,其字符串格式为10-08-2015T13:59:53 + 0000 I need to convert it in the date with Date formatter with Format "dd-MM-yyyy'T'HH:mm:ssZZZ" and this is my method to convert string to date:- 我需要使用日期格式为“ dd-MM-yyyy'T'HH:mm:ssZZZ”的日期格式将其转换为日期,这是我将字符串转换为日期的方法:-

func dateFromString(dateString:String)->NSDate
    {

        print(dateString)
        var dateFormatter = NSDateFormatter()

        //yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ
        dateFormatter.dateFormat = "dd-MM-yyyy'T'HH:mm:ssZZZ"

        var date = dateFormatter.dateFromString(dateString)
        print(date)
        return date!
    }

Sorry I am editing the Qusetion. 抱歉,我正在编辑Qusetion。 I found why it is happening. 我发现了为什么会这样。 In the setting page of iphone In date & time if 24_hour_time is On It is working fine.If it is off Then only the date formatter is giving nil date.I inserted the GMT in that.It is working fine in 8.1.3 but giving problem in 8.4 在iphone的设置页面中,如果24_hour_time为打开,则日期和时间正常,如果没有,则只有日期格式器给出nil date。我插入了格林尼治标准时间。在8.1.3中,它可以正常工作,但给出8.4中的问题

 dateFormatter.timeZone = NSTimeZone(name: "GMT")

Thanks Happy coding 谢谢快乐编码

I found the answer of this question. 我找到了这个问题的答案。 Sorry for the delay to post the answer. 很抱歉延迟发布答案。 You need to enter NSLocale to the formatter as I have done in this example. 您需要像本例中一样将NSLocale输入格式化程序。

func dateFromstring(dateString:String)->NSDate {
    //print(date)
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy'T'HH:mm:ssZZZ"

    //DateFormatter To change By chakshu
    var enUSPOSIXLocale:NSLocale=NSLocale(localeIdentifier: "en_US_POSIX")
    dateFormatter.locale=enUSPOSIXLocale
    dateFormatter.timeZone = NSTimeZone(name: "GMT")
    //dateFormatter.timeZone = NSTimeZone.localTimeZone()

    var date = dateFormatter.dateFromString(dateString)
    //print(date)
    return date!
}

Hope this help. 希望能有所帮助。 Thanks 谢谢

Warning 警告

  • Your function name for the formatter is the same as that of your custom function. 格式化程序的函数名称与自定义函数的函数名称相同。 Not saying this is your problem but something that may not have been intended. 并不是说这是您的问题,但可能不是您想要的。
  • You are using var instead of let in some cases, again, not gonna cause an error 再次,您使用var而不是let,不会导致错误

Otherwise, your code returns the correct date... but in Date format, not String. 否则,您的代码将返回正确的日期...,但采用日期格式,而不是字符串。

To print this in your desired format just convert the Date back to the desired String format: 要以所需格式打印,只需将Date转换回所需的String格式:

func dateFromString(dateString:String)->NSDate
    {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd-MM-yyyy'T'HH:mm:ssZZZ"
        let date = dateFormatter.dateFromString(dateString)
        return date!
    }

let formatter = NSDateFormatter()
formatter.dateFormat = "dd/M/yyyy, H:mm"

print(formatter.stringFromDate(dateFromString("10-08-2015T13:59:53+0000")))

Run here to test on latest Swift... no issues 在这里运行以测试最新的Swift ...没有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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