Problem in setting nsdate value from a string - iphone

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000

Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.

This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.

It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33

You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.

Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)

Related

Convert NSString to NSDate not correct

in my iPhone application I need to convert NSString to NSDate with formaT. Here is my code:
+(NSDate*)dateFromJsonString:(NSString*)string{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
NSLog(#"%#", [dateFormat dateFromString:string]);
return [dateFormat dateFromString:string];
}
If parameter "string" is 2013-05-30T15:53:02 after converting - [dateFormat dateFromString:string] it becomes 2013-05-30 12:53:02 +0000 , so there is a difference in 3 hours. How can I fix it?
You need to set the correct time zone. Here is a sample, correct it to the time zone of your server:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
return [dateFormatter dateFromString:dict[#"completed_at"]];
EDIT: You can check the available time zones with a class method on NSTimeZone:
+ (NSArray *)knownTimeZoneNames
Just output it to the console, and choose the time zone that corresponds to the server one.
EDIT 2: Probably this is more useful:
+ (NSDictionary *)abbreviationDictionary
Choose your time zone and than just change the #"UTC" in the code above with the desired abbreviation.
Try this dateFormat. Since timezone is not given in dateString, dateFormatter assumes it to be local timezone. If you know the timezone set it to formatter.
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
Check this answer for more details.
This is TimeZone issue so use this code to fix this problem :
+(NSDate*)dateFromJsonString:(NSString*)string
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#", [dateFormat dateFromString:string]);
return [dateFormat dateFromString:string];
}
Hope it helps you.
You can use the following code to set timezone.
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
But before you try that, i should tell you that if you are logging the date, it will show it in UTC format and not in the timezone of your computer. So dont check your date by printing it. So try converting the date to string and printing it to check if the date is correct or not.
Try logging NSLog(#"%#",[dateFormatter stringFromDate:date]); first
You just need to set current time zone for date formatter. Because of it, you got some time difference. Use below code..
+(NSDate*)dateFromJsonString:(NSString*)string
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[inputFormatter setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss"];
NSDate *xExpDate = [inputFormatter dateFromString:string];
return xExpDate;
}

NSDateFormatter formatting issue

I am trying to convert NSString into NSDate in 12 hours format. (in iOS 6)
Code :
NSString *Bdt = #"05/23/2012 08:00 AM"
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSLog(#"%#",bd);
Output:
2012-05-23 08:00:00 +0000 it should be 2012-05-23 08:00 AM
Whats wrong in the code ?
Thanks
If you want date in 2012-05-23 08:00 AM style
Create a dateformatter and setDateFormat as yyyy-MM-dd hh:mm a
NSString *Bdt = #"05/23/2012 08:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a" ];
NSString *datestr=[dateFormatter stringFromDate:bd];
NSLog(#"%#",datestr);
*Note : I am using ARC, so objects are not released/autoreleased.
EDIT:
NSDate will be in this format ONLY : 2012-05-23 08:00:00 +0000.
For any other format you need to use NSString.
Nothing is wrong with it. You are printing an NSDate in NSLog which is very different than creating an NSString with a specific format.
It seems your confusing the internal NSDate representation with string formatting. NSDate stores the date internally in a way different from how it is represented by humans. Just like NSString stores strings in a format that may not be what you ultimately want it encoded as, eg. ASCII or UTF-8. When you are calling NSLog you are getting a diagnostic message showing the date according to the string returned by - (NSString *)description or possibly - (NSString *)debugDescription.

Parsing custom string gives wrong date

I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code

NSDate from NSString gives null result

I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.

How to get yyyy-mm-dd hh:mm:ss format from 28 Jul 2011 22:33:57 in iPhone?

I have stuck in issue in which i have to convert date format is Thu, 28 Jul 2011 22:33:57 +0000 into yyyy-mm-dd hh:mm:ss
please give me some idea how to do this?
What you want is to use the NSDateFormatter. Something like this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString* dateString = [f stringFromDate:date];
Do note that the hours will still follow the users selected locale. Use kk:mm:ss to enforce a 24-hour time.
what have you tried? it's difficult to answer questions like this...
first you have to parse the date into an NSDate, use an NSDateFormatter, the incoming format looks like POSIX date format so should be easy.
then you want to output to the format you specify with another NSDateFormatter
You need to use an NSDateformatter to convert the first date to a string with the following syntax.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]]; //Put whatever date you want to convert
Then if you want the date as an NSDate and you have the string generated above just put the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSDate * date = [dateFormatter dateFromString: dateString]; //String generated above