Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a datestyle in Swift

Tags:

swift

I want to set a datestyle in Swift. I've already done this in Objective-c, this is the objective-c code.

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

Now I want this in swift. I already come up with this:

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

But I can't figure out how to set the date style in Swift. With NSDateFormatterMediumStyle

like image 988
Bas Avatar asked Jun 07 '14 19:06

Bas


3 Answers

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

Here is the Swift code: The following code loops through each of these and displays the resulting Date_Time string:

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)
    }
}
like image 128
Vic W. Avatar answered Nov 16 '22 02:11

Vic W.


Thanks to type inference you can do this

dateFormatter.dateStyle = .medium
like image 21
Infinity Avatar answered Nov 16 '22 00:11

Infinity


you can also use this

 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;
like image 28
Vikram Pote Avatar answered Nov 16 '22 00:11

Vikram Pote