2014-06-07 9 views
7

Chcę ustawić styl dat w Swift. Zrobiłem to już w Objective-c, to jest kod celu-c.Ustaw styl dat w 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]; 

Teraz chcę tego w szybkim tempie. Już wymyśliłem to:

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

Ale nie mogę wymyślić, jak ustawić styl daty w Swift. Z NSDateFormatterMediumStyle

+0

mam pojęcia jak, ale starają się bawić z metody 'toRaw()' 'na NSDateFormatterMediumStyle' – Ossir

Odpowiedz

14

Dzięki wnioskowanie typu można to zrobić

dateFormatter.dateStyle = .MediumStyle 
+0

Dziękuję bardzo, to było dokładnie to, czego szukasz! – Bas

+3

Należy pamiętać, że odpowiedź @ Dash jest pełną odpowiedzią. W tym przypadku dataStyle jest znana z typu 'NSDateFormatterStyle', więc część tę można pominąć. –

+0

zobacz: http://nsdateformatter.com, aby uzyskać więcej informacji – Lifely

9
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle 
8

można również użyć tego

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

w Swift 3, sintaxis zmieniło się trochę, nie NS prefiks i wyliczanie małych liter:

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

Bądź ostrożny, że formater użyje domyślnie strefę czasową swojego regionu urządzenia, w niektórych zastosowaniach chcesz zdefiniować ręcznie poprzez własności czasowej w formater czy można określić format chcesz

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 

Oto Swift code: następujący kod pętli przez każdego z nich i wyświetla odpowiedni Date_ Czas ciąg:

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