-3

I have one string

NSString *timeStr =@"04:57PM";

This is my date formatter

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mma"];    

Converting NSString to NSDate

NSDate *date=[dateFormat dateFromString:timeStr];
NSLog(@"date is :::%@",date); 

The output in the Log is

date is :::1970-01-01 11:27:00 +0000.

Can i know What to do to get the output as date is :::04:57PM.

Bhavin
  • 27,155
  • 11
  • 55
  • 94
Murali
  • 188
  • 1
  • 11
  • refer this http://stackoverflow.com/questions/576265/convert-nsdate-to-nsstring – Sugan S May 10 '13 at 10:38
  • You have created date, but printing date1. Is it correct? :) – Volodymyr B. May 10 '13 at 10:39
  • it is date..now i edited help me – Murali May 10 '13 at 10:41
  • you can set dateformat object to timezone then u will get exact result what u expected. [dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; – KAREEM MAHAMMED May 10 '13 at 10:43
  • 1
    just do `NSLog(@"date is :::%@", timeStr);`, why do you need to convert the string to date to print it back as a string in the same format it was previously? – tkanzakic May 10 '13 at 10:50
  • @sugan ..i need datefromString convertion but not stringfromdate – Murali May 10 '13 at 10:51
  • @tkanzakic my label shows time ..i have time button next to label .on click of time i need to show time picker with the time in the label.so to set date to picker i need NSDate instance.So i need to convert it to date. – Murali May 10 '13 at 10:56
  • please do not give negative vote. may be for you guys it looks like a simple question. – Murali May 10 '13 at 11:00

4 Answers4

1

You need to use the same NSDateFormatter and the stringFromDate method to get the same string. Your date will probably be different because you haven't set the locale on your NSDateFormatter.

Adis
  • 4,512
  • 2
  • 33
  • 40
1

You can't. The reason why you can't is that internally NSDate is nothing more than a simple double representing the amount of time that has passed since a reference point. It only becomes 4:57 PM when you want to convert it back into a string using NSDateFormatter

Think of it this way, a point in time is the same all over the world. It is just measured differently depending on where you are. NSDate is that point, and NSDateFormatter helps you get the final measurement. It will always log according to GMT.

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • Thnaks for your answer..i have one question that is....i have label shows time ..i have time button next to label .on click of time i need to show time picker with the time in the label.so to set date to picker i need NSDate from NSString instance.So i need to convert it to date..How to do this – Murali May 10 '13 at 11:23
  • You already did. What is the problem? Did you forget to set a timezone on your date picker? – borrrden May 10 '13 at 11:29
-2

instead of this

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"hh:mma"];

Use this

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"hh:mm a"];
Ahmed Z.
  • 2,329
  • 23
  • 52
-2

You try the following code:

Replace

[dateFormat setDateFormat:@"hh:mma"];

with

[dateFormat setDateFormat:@"h:mm a"];

Hope this will help you.

pankaj asudani
  • 862
  • 6
  • 18