2

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

Mashhadi
  • 3,004
  • 3
  • 46
  • 80

6 Answers6

4

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);
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
2

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);
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
2

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];
Maulik
  • 19,348
  • 14
  • 82
  • 137
2

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];
Community
  • 1
  • 1
mayuur
  • 4,736
  • 4
  • 30
  • 65
2
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh:mm:ss  a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];    
NSLog(@"time, %@",theTime);
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
1

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];
Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
  • why need 2 date formatters? You can use the first one itself ?? – Lithu T.V Oct 04 '12 at 07:34
  • @LithuT.V frist date formate is use the the date string value converted the date. and after use the second is use the date value converted the required formate. – Vikas S Singh Oct 04 '12 at 08:26
  • See here you are defining 2 dateformatters- formatter and dateformatter1.What is the need of that? The "formatter' can be used for the purpose of converting to date and after that set it with new format and timezone for next conversion to string.Isn't it enough? – Lithu T.V Oct 04 '12 at 08:57
  • @LithuT.V yes i know but you can write the one code in the different -2 type , this is not a issue. – Vikas S Singh Oct 04 '12 at 09:02