2014-06-07 17 views
7

Tôi muốn đặt kiểu dữ liệu trong Swift. Tôi đã làm điều này trong Objective-c, đây là mã mục tiêu-c.Đặt kiểu dữ liệu trong Swift

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]; 

Bây giờ tôi muốn điều này nhanh chóng. Tôi đã đưa ra điều này:

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)" 

Nhưng tôi không thể tìm ra cách đặt kiểu ngày trong Swift. Với NSDateFormatterMediumStyle

+0

Không có ý tưởng như thế nào, nhưng cố gắng để chơi xung quanh với phương pháp 'toRaw() 'trên' NSDateFormatterMediumStyle' – Ossir

Trả lời

14

Nhờ gõ suy luận bạn có thể làm điều này

dateFormatter.dateStyle = .MediumStyle 
+0

Cảm ơn bạn rất nhiều, điều này là chính xác những gì tôi đang tìm kiếm! – Bas

+3

Lưu ý rằng câu trả lời của @ Dash là câu trả lời đầy đủ. Trong trường hợp này dateStyle được biết là thuộc kiểu 'NSDateFormatterStyle', để phần đó có thể được bỏ qua. –

+0

xem: http://nsdateformatter.com để biết thêm – Lifely

9
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle 
8

bạn cũng có thể sử dụng này

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; 
1

Trong nhanh chóng 3, sintaxis đã thay đổi một chút, không có tiền tố NS và liệt kê chữ thường:

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)) 

Hãy cẩn thận rằng formatter sẽ sử dụng theo mặc định múi giờ của khu vực thiết bị của bạn, đối với một số ứng dụng mà bạn muốn xác định bằng tay thông qua thuộc tính múi giờ trong định dạng hoặc bạn có thể chỉ định các định dạng mà bạn muốn

0

Swift 3:

dateFormatter.dateStyle = .medium 
2

Swift 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 

đây là mã Swift: các mã sau vòng qua từng và hiển thị kết quả Date_ Hiện chuỗi:

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) 
    } 
} 
Các vấn đề liên quan