get time and date by NSDate

38,328

Solution 1

NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"MM/dd/yy";

NSString *dateString = [dateFormatter stringFromDate: localDate];



NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = @"HH:mm:ss";


NSString *dateString = [timeFormatter stringFromDate: localDate];

There are many many different ways to represent the date and time so check NSDateFormatter for more info.

Solution 2

i would go with using NSDateComponents and NSCalendar if you really want to have the values for the minute, hour and the date ... Something like this:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                                                   fromDate:[NSDate date]];

then you can use the properties of the components to access the value of each components with component.hour, component.year, etc.

Also please note that if you are going with the NSDateFormatter example - then you should remember that NSDateFormatter is a heavy object and you should really reuse it if possible ...

Solution 3

I'd use the following over the other answers:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSString *formattedDateString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];

The advantage it has is that it will set the formatting to what the user has set in their system settings. That way you don't have to worry about items like if the user prefers their short dates in the format 10/31/11 or 31/10/11. See the documentation for a list of styles that are available.

Swift 2.2:

let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = .NoStyle
let myDateString = dateFormatter.stringFromDate(myNSDate)

dateFormatter.dateStyle = NSDateFormatterStyle.NoStyle
dateFormatter.timeStyle = .ShortStyle
let myTimeString = dateFormatter.stringFromDate(myNSDate)

Solution 4

In Swift you can enjoy yourself with extension, for instance:

// MARK: - NSDate extension -

extension NSDate {

    func toString() -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd MMMM yyyy - HH:mm"
        return dateFormatter.stringFromDate(self)
    }

    func getTime() -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        return dateFormatter.stringFromDate(self)
    }

    func getDate() -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy"
        return dateFormatter.stringFromDate(self)
    }
}

And then, anywhere in your code you can do:

print("Time right now: \(NSDate().getTime())") 

Solution 5

SWIFT 4 version of @McCygnus answer

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
let myDateString = dateFormatter.string(from: Date())

dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
let myTimeString = dateFormatter.string(from: Date())
Share:
38,328
Safari
Author by

Safari

Updated on April 25, 2020

Comments

  • Safari
    Safari about 4 years

    how can I retrieve the date and time from a NSDate. I want to see them separately. thanks