简体   繁体   English

Swift如何将字符串转换为NSDate

[英]Swift how to convert string to NSDate

I've got a date stored in my database and I want to retrieve it and display it nicely in my tableview cells. 我已经在数据库中存储了一个date ,我想对其进行检索并将其很好地显示在我的tableview单元格中。

The format it comes in from the database and stored in option1 is BO05151530 它来自数据库并存储在option1中的格式为BO05151530

Where the first two letters have meaning in the program but are not needed for the date so I take those off using the substringFromIndex function. 前两个字母在程序中具有含义,但日期不需要,因此我使用substringFromIndex函数将其substringFromIndex

So what is left is 05151530 where it represents MMddhmm 所以剩下的是05151530 ,它表示MMddhmm

I would like to display it nicely like MM-dd @ h:mm a 我想很好地显示它,例如MM-dd @ h:mm a

For example 12-05 @ 3:45 am 例如12-05 @ 3:45 am

Here is what I tried but unfortunately ns_date1 comes up as nil each time. 这是我尝试过的方法,但不幸的是ns_date1都显示为nil。

What would you suggest I do? 你会建议我做什么?

let date1 = option1.substringFromIndex(option1.startIndex.advancedBy(2))

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMddhhmm"

let ns_date1 = dateFormatter.dateFromString(date1)

Try this. 尝试这个。 you don't need to separate BO NSDateFormatter can handle extra string 您不需要分开BO NSDateFormatter可以处理额外的字符串

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "'BO'MMddHHmm"

let ns_date1 = dateFormatter.dateFromString("BO05151530")

dateFormatter.dateFormat = "'BO' MM-dd @ hh':'mm a"

let string = dateFormatter.stringFromDate(ns_date1!)

try 尝试

dateFormatter.dateFormat = "MMddHHmm"

HH is 24 hour format and hh is 12 hour format. HH是24小时格式,hh是12小时格式。 you need the 24 one 你需要24个

Check out this app, this will help you know the right format and give you code 签出此应用程序,这将帮助您了解正确的格式并为您提供代码

https://itunes.apple.com/ae/app/date-format-creator/id965645209?mt=12 https://itunes.apple.com/ae/app/date-format-creator/id965645209?mt=12

在此处输入图片说明

NOTE: you need to add year to get a correct NSDate 注意:您需要添加年份才能获得正确的NSDate

I have this function : 我有这个功能:

class func getTheDateString(stringForInputDate: String, fromFormat inputFormat: String, toFormat outputFormat: String) -> String {
let formatter: NSDateFormatter = NSDateFormatter()
formatter.dateFormat = inputFormat
formatter.timeZone = NSTimeZone.localTimeZone()
let dateForInput: NSDate = formatter.dateFromString(stringForInputDate)!
formatter.dateFormat = outputFormat
return formatter.stringFromDate(dateForInput)

} }

and used it as: 并将其用作:

let stringDate : String = "0515930" // "05151530"

    if stringDate.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) < 8 {

        print([ViewControllerForScreen1 .getTheDateString(stringDate, fromFormat: "MMddHmm", toFormat: "MM-dd @ h:mm a")]);

    }else {

        print([ViewControllerForScreen1 .getTheDateString(stringDate, fromFormat: "MMddHHmm", toFormat: "MM-dd @ h:mm a")]);

    }

OUTPUT: OUTPUT:

  1. ["05-15 @ 9:30 AM"] [“ 05-15 @ 9:30 AM”]

  2. ["05-15 @ 3:30 PM"] [“ 05-15 @ 3:30 PM”]

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

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