NSDate in string shows date of one month before - ios

I am taking date of string having format yyyy-MM-DD from database then converting it into NSDate having format yyyy-MM-DD. And again converting back it into string format dd-MM.
But when at last I get date in string format it shows one month before date.
Here is the code I have used:
NSDateFormatter *format=[[NSDateFormatter alloc]init];
NSDateFormatter *format2=[[NSDateFormatter alloc]init];
NSString *date;
[format setDateFormat:#"yyyy-MM-DD"];
[format2 setDateFormat:#"DD-MMM"];
dict=[[NSMutableDictionary alloc]init];
for(int i=0;i<[viewHistoryData count];i++)
{
dict=[viewHistoryData objectAtIndex:i];
date=[dict objectForKey:#"Date"];
NSLog(#"My date with out format = %#",date);
NSString *dateString =[format2 stringFromDate:[format dateFromString:date]];
NSLog(#"My date is = %#",dateString);
[tempArray addObject:dateString];
}
OUTPUT
2014-02-07 15:01:07.586 VirtualRunner-V3[3580:c07] My date with out format = 2014-02-07
2014-02-07 15:01:07.588 VirtualRunner-V3[3580:c07] My date is = 07-Jan
Does anybody know how to solve this?

You are using the wrong placehodler for day of month. What you are using is Day of Year ranging from 1 ... 365. So instead of
[format setDateFormat:#"yyyy-MM-DD"];
use
[format setDateFormat:#"yyyy-MM-dd"];
See this link for a complete overview.

The format DD representes day of the year, not day of the month, so change your formatters to this:
[format setDateFormat:#"yyyy-MM-dd"];
[format2 setDateFormat:#"dd-MMM"];

Correct the two format.
[format setDateFormat:#"yyyy-MM-dd"];
[format2 setDateFormat:#"dd-MMM"];
But I think, Issue related with timezone. Use below to format.
[format2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
See this link

Related

Issue with formatting date

I know this question has been asked so many time and may be duplicate of some question, actually i am trying this for storing Date into array by converting them in String. I need that Value in NSDate format so i again convert that stored string into Date.
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"MM-dd-yy-hh-mm-ss"];
NSString *date = [dateformat stringFromDate:datePicker.date];
[kAppDelegate.glbArrName addObject:date];
But I get this output :
NSString *date = [kAppDelegate.glbArrDate objectAtIndex:indexPath.row];
NSLog(#"Date of birth %#",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM-dd-yy-hh-mm-ss"];
NSDate *birthDate = [[NSDate alloc] init];
birthDate = [dateFormatter dateFromString:date];
NSLog(#"Date of birth after formatting %#",birthDate);
Output is:
Date of birth 04-05-16-06-38-14
Date of birth after formatting 2016-04-05 01:08:14 +0000
Why it changes format, as i have done same as previous. please help me find out ..
You're rewriting the date string back into an NSDate, then printing out the NSDate, which defaults to the latter 2016-04-05 01:08:14 +0000 format as part of NSDate's -description.

Problems converting a string date to NSDate

I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);

SQLite storing, retrieving and comparing a DATETIME field

I am really stuck trying to compare dates in SQLite queries in Objective C. Here's what I'm doing:
Storing the date:
This document tells me to use the dateformat specified below, but it doesn't seem right. I tried using yyyy-MM-dd hh:mm:ss without success too though.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSString *dateString=[dateFormat stringFromDate:today];
NSString *query = [NSString stringWithFormat: #"INSERT INTO user (edited) VALUES (\"%#\")", dateString];
Comparing the date
I am using a timestamp for this, which I convert to a datetime string
long stamp = [sessie integerForKey:#"stamp"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSString *dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:stamp]];
sqlite3_stmt *result = [db selectQuery:[NSString stringWithFormat:#"SELECT * FROM user WHERE edited > '%#'", dateString]];
The timestamp is simply generated using [[NSDate date] timeIntervalSince1970]. The problem is that the query won't give the correct results, and I don't even know if the date is stored correctly in the first place. Any help would be greatly appreciated!
A couple of observations:
For your date string, you do definitely do not want to use YYYY-MM-DD HH:MM:SS. That will not generate a valid date string. Using yyyy-MM-dd hh:mm:ss is much closer, but not quite right, either (since you'll use 12-hour hh). Use yyyy-MM-dd HH:mm:ss instead.
This date format, though, does not capture time zone, so, if you store date strings in your SQLite database, you should use UTC (GMT) as discussed in the SQLite Date And Time Functions documentation.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSString *dateString = [formatter stringFromDate:today];
As shown above, you probably want to also specify the locale so that the format of the dates will not change depending upon the localization settings of the device.
Note that you might consider using timeIntervalSince1970 to store the date as a REAL value in your database (as opposed to a TEXT). This can be a little more efficient and automatically addresses the UTC issue.

Date format of Tapku Calendar

In the API , the selected day format is like 2013-11-01T23:00:00%2B0000
But my date format is like this: 2013-11-01T23:00:00+0000
To convert, I have used below code:
NSString *plus = [NSString stringWithFormat:#"%#%#", [timeStamp substringToIndex:[timeStamp length]-5], #"%2B0000"];
Instead of (+) the unicode of plus which is (%2B).
But when I select the day from Tapku calender, I receive the error which says date format is wrong. How can I fix this problem?
NSDate *tempDate=toDate;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss zzzz"];
[periodOfInspectionToTextField setText:[NSString stringWithFormat:#"%#",[df stringFromDate:d]]];
toDate=[df dateFromString:periodOfInspectionToTextField.text];
You need to set the dateformat of tapku which is below:-
[YYYY-MM-DD hour:minute:second];
Also follow this link for more details.
Tapku library -what is the date format in day view
First, you need to unescape the date string from the API to remove the percent escapes:
NSString *dateString = [#"2013-11-01T23:00:00%2B0000" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
This will give a date string of 2013-11-01T23:00:00+0000. Then you need to parse this string into a NSDate object:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:sszzzz"];
NSDate *date = [formatter dateFromString:dateString];

Using NSDateFormatter to format date to local date

I have tried numerous attemps of formatting a relatively simple date format to a date object, but also converting to my local time zone.
All dates are formatte like this: 2010-09-11T08:55:00. This is GMT time, and I want it converted to a date object with GMT+2.
Can anyone please point me in the right direction?
I just figured out how to format the date:
NSString *str = #"2010-09-10T18:24:43";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [format dateFromString:str];
[format setDateFormat:#"EEEE MMMM d, YYYY"];
str = [format stringFromDate:date];
[format release];
NSLog(#"%#", str);
Just need to convert to local timezone now..

Resources