Mashhadi
Mashhadi

Reputation: 3024

Get time from NSDate returns nil

I am trying to show time on graph and i have a full time stamp in this @"2012-08-28 18:50:24" format. when i try to get time from this date then it returns nil in NSDate.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];

in this code only if I change [formatter setDateFormat:@"HH:mm:ss"]; line to [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; then it starts working with full date and time. But i need only time

Upvotes: 2

Views: 2202

Answers (6)

ChintaN -Maddy- Ramani
ChintaN -Maddy- Ramani

Reputation: 5164

u have string . so you must have to convert it into date first as per your string format. after that you can convert date into any format. so follow this.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *str = [formatter stringfromdate:time];
NSLog(@"%@",str);

Upvotes: 4

Rajneesh071
Rajneesh071

Reputation: 31091

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh:mm:ss  a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];    
NSLog(@"time, %@",theTime);

Upvotes: 2

Vikas S Singh
Vikas S Singh

Reputation: 1766

First you have converted your string date and time in the date formate..

YourTimeStr = @"2012-08-28 18:50:24";  

  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-mm-dd HH:mm:ss";
    NSDate *DateAndTime = [formatter dateFromString:LogOutTimeSTR];

then

NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter1 setDateFormat:@"HH:mm:ss"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

    NSString *YourTimeStr =[dateFormatter1 stringFromDate:DateAndTime];

Upvotes: 1

mayuur
mayuur

Reputation: 4746

If you need to produce Date from String then you'll need to set the date format accordingly.

This is important that you create a NSDate object first! Only then you can separate time from whole date.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];

then

[formatter setDateFormat:@"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:time];

You can also use other string concatenation functions to remove the Date Modules like this.

However, if you have an NSDate first, then your previous code would work.

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:date];

Upvotes: 2

Maulik
Maulik

Reputation: 19418

Because, first you need to convert your string into NSDate in the right formate that is

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:@"2012-08-28 18:50:24"];

Now you can reformate the NSDateFormatter according to your need. Now you can get time from the

[formatter setDateFormat:@"HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:aDate];

Upvotes: 2

Lithu T.V
Lithu T.V

Reputation: 20021

NSDateFormatter is used to format your NSDate to your requirements. Here you are fetching the date from a string so you have to tell the NSDateFormatter to look for the string with that format and convert it into date

NSDate contains both date and time so if you want the time only to show use dateformatter to get the time component from NSDate

iOS manages time and date together in NSDate.Actually they are supposed to work together.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];   
   // GET  Proper NSDate 
    NSDate *time = [formatter dateFromString:@"2012-08-28 18:50:24"];        
    //To your requirement
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *timestr=[formatter stringFromDate:time];
    NSLog(@"%@",timestr);

Upvotes: 2

Related Questions