user3745888 user3745888 - 3 months ago 11
Swift Question

How to convert this code of date to swift?

I have this code in Objective-C:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
NSString* dateTimePrefix = [formatter stringFromDate:[NSDate date]];


I'm trying to convert to Swift, but I don't know any lines this is my try:

var formatter = NSDateFormatter()
formatter.dateFormat="yyyy-MM-dd-HH-mm-ss"
var dateTimePrefix : NSString
dateTimePrefix = formatter.stringFromDate(formatter as? NSData)


I get errors in
stringFromDate
. What is the problem?

Answer

that would be the same code in Swift:

var formatter: NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
let dateTimePrefix: String = formatter.stringFromDate(NSDate())