In Objective C, we could easily assign present date to NSString like string = [NSDate date];
.
Could someone help me how to assign the same in Swift ? Thanks in advance.
First off, your statement...
In Objective C, we could easily assign present date to NSString like string = [NSDate date];
Is complete rubbish.
In Objective-C you need to use an NSDateFormatter
to render an NSString
out of an NSDate
.
You would do it something like this...
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle
NSString *string = [df stringFromDate:date];
Now, in Swift, not surprisingly, it is EXACTLY the same.
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
let string = dateFormatter.stringFromDate(date)
maybe you want this:
let string = NSDate().description
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With