在Swift中设置日期样式

时间:2014-06-07 19:32:59

标签: swift

我想在Swift中设置日期风格。 我已经在Objective-c中完成了这个,这是Objective-c代码。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateStyle:(NSDateFormatterMediumStyle)];

NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];

[timeFormatter setDateFormat:@"h:mm a"];

NSString *fireDate = [dateFormatter stringFromDate:notification.fireDate];
NSString *fireTime = [timeFormatter stringFromDate:notification.fireDate];
NSString *dateTime = [NSString stringWithFormat:@"%@ - %@", fireDate, fireTime];

现在我想要迅速。我已经想到了这个:

var dateFormatter = NSDateFormatter()
var timeFormatter = NSDateFormatter()

timeFormatter.dateFormat = "h:mm a"
//code to set date style.....
var fireDate: NSString = dateFormatter.stringFromDate(notification.fireDate)
var fireTime: NSString = timeFormatter.stringFromDate(notification.fireDate)
var dateTime: NSString = "\(fireDate) - \(fireTime)"

但我无法弄清楚如何在Swift中设置日期样式。使用NSDateFormatterMediumStyle

6 个答案:

答案 0 :(得分:14)

感谢类型推断,你可以这样做

dateFormatter.dateStyle = .medium

答案 1 :(得分:9)

dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

答案 2 :(得分:9)

斯威夫特4:

DateFormatter() has 5 format style options for each of Date and Time. These are:
.none .short .medium .long .full

 DATE      TIME     DATE/TIME STRING
"none"    "none"    
"none"    "short"   9:42 AM
"none"    "medium"  9:42:27 AM
"none"    "long"    9:42:27 AM EDT
"none"    "full"    9:42:27 AM Eastern Daylight Time
"short"   "none"    10/10/17
"short"   "short"   10/10/17, 9:42 AM
"short"   "medium"  10/10/17, 9:42:27 AM
"short"   "long"    10/10/17, 9:42:27 AM EDT
"short"   "full"    10/10/17, 9:42:27 AM Eastern Daylight Time
"medium"  "none"    Oct 10, 2017
"medium"  "short"   Oct 10, 2017, 9:42 AM
"medium"  "medium"  Oct 10, 2017, 9:42:27 AM
"medium"  "long"    Oct 10, 2017, 9:42:27 AM EDT
"medium"  "full"    Oct 10, 2017, 9:42:27 AM Eastern Daylight Time
"long"    "none"    October 10, 2017
"long"    "short"   October 10, 2017 at 9:42 AM
"long"    "medium"  October 10, 2017 at 9:42:27 AM
"long"    "long"    October 10, 2017 at 9:42:27 AM EDT
"long"    "full"    October 10, 2017 at 9:42:27 AM Eastern Daylight Time
"full"    "none"    Tuesday, October 10, 2017
"full"    "short"   Tuesday, October 10, 2017 at 9:42 AM
"full"    "medium"  Tuesday, October 10, 2017 at 9:42:27 AM
"full"    "long"    Tuesday, October 10, 2017 at 9:42:27 AM EDT
"full"    "full"    Tuesday, October 10, 2017 at 9:42:27 AM Eastern Daylight Time

这是Swift代码: 以下代码遍历其中的每一个并显示生成的Date_Time字符串:

import Foundation
let dateTimeFMT = DateFormatter()
var dateTimeString: String = ""
let currentDateTime = Date()
var stylesAsInts: [String] = [ // pre-formatted for display
    "\"none\"  ", "\"short\" ", "\"medium\"", "\"long\"  ", "\"full\"  "
] 
var dateStyleIdx: Int;    var timeStyleIdx: Int

print(" DATE      TIME     DATE/TIME STRING")
// LOOP through ALL time and date styles (.none .short .medium .long .full)
// these five values equate to 0...4
for dateStyleIdx     in 0...4 { // loop on DATE...
    dateTimeFMT.dateStyle     = DateFormatter.Style(rawValue: UInt(dateStyleIdx))!
    for timeStyleIdx in 0...4 { // loop on TIME...
        dateTimeFMT.timeStyle = DateFormatter.Style(rawValue: UInt(timeStyleIdx))!
        dateTimeString      = dateTimeFMT.string(from: currentDateTime)
        print("\(stylesAsInts[dateStyleIdx])  \(stylesAsInts[timeStyleIdx]) ", dateTimeString)
    }
}

答案 3 :(得分:8)

您也可以使用此

 let formatter = NSDateFormatter()

 //format mm-dd-yy like 02-23-15
 formatter.dateStyle = .ShortStyle;


 //format yyyy-MM-dd like 2015-02-23
 formatter.dateFormat = "yyyy-MM-dd";


 //format feb 02, 2015 
 formatter.dateStyle = .MediumStyle;

答案 4 :(得分:1)

在swift 3中,sintaxis已经改变了一点,没有NS前缀和小写枚举:

let myDate = Date()
let dateFormatter = DateFormatter()
//affects only the date representation of your date object
// xcode will let you know all the other available options
dateFormatter.dateStyle = .short
//the time part has it's own property (.short, .long, .none)
dateFormatter.timeStyle = .none

//01-27-17
print(dateFormatter.string(from: myDate))

请注意格式化程序默认使用设备区域的时区,对于您通过格式化程序中的timezone属性手动定义的某些应用程序,或者您可以指定所需的格式< / p>

答案 5 :(得分:0)

斯威夫特3:

dateFormatter.dateStyle = .medium