我将如何转换" 2015年9月6日"到Swift中的NSDate()?

时间:2015-09-09 14:06:46

标签: swift nsdate nsdateformatter

我将如何转换" 2015年9月6日及#34;到Swift中的NSDate()?

3 个答案:

答案 0 :(得分:3)

所以我假设你有一个String,你想把它转换为NSDate

最简单的方法是通过NSDateFormatter。

查看this网站,了解如何使用日期输入语法和文档here

let dateToConvert = "6 September, 2015"

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "d MMMM, yyyy"
let date = dateFormatter.dateFromString(dateToConvert)   //"Sep 6, 2015, 12:00 AM"

祝你好运!

答案 1 :(得分:1)

如评论所述,我建议您查看文档,因为它们与Objective-C的步骤相同。

您可以使用dateFromString

NSDate

获得mm-dd-yyyy后,您可以为<?php // let's make it more unlikely you miss an error condition/message $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $category = $_POST['cat']; $var = "_one"; $category = $category . $var; // never mix mysql_* functions with pdo // $category = wrong_mysql_real_escape_string_wrong($category); // make category a named parameter of the statement like all the other parameters // doesn't make sense to "intermix" this one.... $stmt = $pdo->prepare(' SELECT * FROM my_table WHERE id = :id AND category_name = :category LIMIT :start, :limit '); // you're sure all the variables exist at this point and contain what you expect? $params = array( 'id'=>$id, 'category'=>$category, 'start'=>$start, 'limit' => $limit ); // let's make sure by printing them ... echo '<pre>executing statement with: ', htmlspecialchars(var_export($params, true)), "</pre>\r\n"; $stmt->execute( $params ); 输出使用其他日期格式化程序。

答案 2 :(得分:1)

你可以这样做。

let dateString = "6 September, 2015"
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-yyyy"

let dateFormat: NSDate? = formatter.dateFromString(dateString)

if let date = dateFormat {
    // Do something with it.
}