31

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

Safari
  • 11,437
  • 24
  • 91
  • 191
  • 1
    When you say "see them", do you mean in a string variable? Or have them in two different NSDate object? For example: `NSDate *time` & `NSDate *date` – Black Frog Apr 17 '11 at 02:13

5 Answers5

50
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.

Saikiran Komirishetty
  • 6,525
  • 1
  • 29
  • 36
Brodie
  • 3,526
  • 8
  • 42
  • 61
  • Be careful, because you will hardcode the format to ""MM/dd/yy" and "HH:mm:ss", preventing it to be localized properly. See answer from McCygnus on how to let the system manage the formatting. – Vitalii Jul 08 '16 at 17:54
18

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 ...

Moszi
  • 3,236
  • 2
  • 22
  • 20
10

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)
Vitalii
  • 4,267
  • 1
  • 40
  • 45
McCygnus
  • 4,187
  • 6
  • 34
  • 30
2

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())") 
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
1

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())
anoop4real
  • 7,598
  • 4
  • 53
  • 56