How to format DateFormatter(NSDateFormattter) for Date String

前端 未结 1 1764
借酒劲吻你
借酒劲吻你 2021-01-23 11:11

How to construct formatter(NSFormatter) for following Date string

Date dateString = \"Sun Oct 30 2016 00:00:00 GMT+0530 (IST)\"

let dateFormatter = DateFormatte         


        
相关标签:
1条回答
  • 2021-01-23 11:54

    I think you'll have to tell DateFormatter to ignore the 'GMT' part, because that throws it off. GMT is implied if you have +0530 though, so you don't need it.

    For this string:

    let dateString = "Sun Oct 30 2016 00:00:00 GMT+0530"
    

    This format works:

    dateFormatter.dateFormat = "E MMM d yyyy HH:mm:ss 'GMT'x"
    

    If the (IST) is always the same, you can tell the date formatter to ignore it too by using

    dateFormatter.dateFormat = "E MMM d yyyy HH:mm:ss 'GMT'x '(IST)'"
    

    If that part can vary, you should probably use String methods to remove that part of the string before parsing the date.

    0 讨论(0)
提交回复
热议问题