user3745888 user3745888 - 1 year ago 55
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()
var dateTimePrefix : NSString
dateTimePrefix = formatter.stringFromDate(formatter as? NSData)

I get errors in
. What is the problem?

Answer Source

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