Date format of Tapku Calendar - ios

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];

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.

NSString to NSDate with format conversation issue

I've already searched StackOverflow.com for an answer to this question, still without any success. Already looked here:
iOS - Converting time and date to user time zone
Date Format - time zone
Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?
IOS how to set date format
Nothing of those worked.
So I have this NSString date format: 2014-05-07T10:28:52.000Z trying to convert it to NSDate, this is what I'm using:
+ (NSDate*)stringToDate:(NSString*)string format:(NSString*)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
return [dateFormatter dateFromString:string];
}
This is how I'm using it:
NSDate *date = [AppUtils stringToDate:youtube.postDate format:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
I've also tried those formats:
yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm.ss.ZZZ
yyyy-MM-dd'T'HH:mm.ssZZZ
How could I convert it to NSDate successfully?
Thanks in advance!
If the date format is fixed, what I do is below.
Replace T by space
Replace Z by blank
And then do formatting...
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
Now do the formatting using
yyyy-MM-dd HH:mm:ss
Edit 1
If you want to work with your case, use below
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Edit 2
I tried this and it is working.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"T" withString:#" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Edit 3
To make working with your case use below.
NSString *dateReceivedInString = #"2014-05-07T10:28:52.000Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"ddddd====%#", [dateFormatter dateFromString:dateReceivedInString]);
Here are 2 methods that I use to convert RFC3339 Date string to NSDate,
And also Method for converting NSDate to RFC3339 Date string
Method for converting RFC3339 Date string to NSDate
+ (NSDate *)dateForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
return [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];}
Method for converting NSDate to RFC3339 Date string
+ (NSString *)RFC3339DateTimeFromDate:(NSDate *)aDate
{
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
return [rfc3339DateFormatter stringFromDate:aDate];
}

IOS - Date formatting from string

I would like to format a date to display in a uitableview custom cell
The data is passed into the app from a CMS - it is provided as a string in the following format and stored in a date type variable-
2014-04-15 10:10:45 +0000
Our app will initially be UK based - so I need to convert the format into DD/MM/YYYY format.
I tried the following code to parse my date (dateadded which is of type date).
NSDateFormatter *formatter = [NSDateFormatter new];
cellRecP.artDate.text = [formatter stringFromDate:resItem.dateadded];
but this just returns null - i guess the date format provided above is not anything that stringfromdate understands - is there any other way to format date?
Use an 'NSDateFormatter'
NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];
with this format to parse the string
[newFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];
and you should get a valid 'NSDate' object
Try This and also Checkout this All Formate It is really good and helpful.
NSString *yourString = #"2014-04-15 10:10:45 +0000";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];//Set Your Timezone
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];//You have to set this formate.
NSDate *dd = [df dateFromString:yourString];//This will convert into date;
//Now you can set your formate.
[df setDateFormat:#"dd/MM/yyyy"];
NSString *str = [df stringFromDate:dd];
You need an NSDateFormatter and a proper unicode parse string. NSDateFormatter by default automatically checks the device locale setting the correct output.
This is an example from some code of mine:
NSDateFormatter *dateWriter = [[NSDateFormatter alloc] init];
dateWriter.dateFormat = #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'ZZZ";
dateWriter.dateStyle = NSDateFormatterMediumStyle;
dateWriter.timeStyle = NSDateFormatterMediumStyle;
Pay attention that date formatters are pretty expensive to create.

Convert NSString back to NSDate

I am using NSDateFormatter to convert the current date to a string (in the format: February 16, 2013). How can I convert this string back to a NSDate object?
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle];
The problem appears to be that the month is written out (February vs. 02), and other questions only explain to use NSDateFormatter with a format such as MM-dd-yyyy, which I do not believe is possible here. Must I parse this date manually, convert February to 02, and go from there?
You can use dateFromString of the same NSDateFormatter class to perform backward conversion.
To make it work you need to define dateStyle, so parser will know how text string should be parsed. For the date style that you provided code below will work:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:#"February 16, 2013"];
NSLog(#"%#", date);
If you want to be able to use localized date formats, you should go with templates
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
NSLog(#"%#", dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:#"MMMdY"
options:0
locale:[NSLocale currentLocale]]];
NSLog(#"%#", [dateFormatter dateFromString:dateString]);
Since you have a fixed format that you wish to parse, you must setup the date formatter with the locale of en_US_POSIX. Then you must set he date format to MMMM dd, yyyy. This will pare any date string that has the full month name, the month day, a comma, then the four-digit year.

Converting NSString to Date

I have two strings: date1 = 3-3-2011;
and i want to convert in to 3-March-2011 and display it in label.
NSString *myString = 3-3-2011;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d-MM-YYYY"];
NSDate *yourDate = [dateFormatter dateFromString:myString];
//now format this date to whatever you need…
[dateFormatter setDateFormat:#"d-MMM-YYYY"];
NSString *resultString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];
but yourdate = 2010-12-25 18:30:00 +0000
resultstring = 26-Dec-2010
i want 3-March-2010
please help!
Thank you.
You can use NSDateFormatter.
Convert your current string to NSDate object, so that you can
convert it into any format you want.
NSString *myString = #"3-3-2011";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d-M-yyy"];
NSDate *yourDate = [dateFormatter dateFromString:myString];
Now you can convert this NSDate to any format you want.
[dateFormatter setDateFormat:#"d-MMMM-yyyy"];
NSString *resultString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];
Apple's documentation on NSDateFormatter is here.
Take a look at NSDateFormatter. In particular, take a look at the dateFromString: and stringFromDate: methods. You'll need to convert your original string to an NSDate, then convert that NSDate to another string.
One tip for using NSDateFormatter: be sure always to set a locale. If you don't manually set a locale, it has a bug regarding 12/24 hour clock settings. The example code on the page I linked to shows how to set a locale.
Use d-M-Y (or M-d-Y, depending on which is the month):
NSString *myString = 3-3-2011;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d-M-Y"];
NSDate *yourDate = [dateFormatter dateFromString:myString];
And see the Unicode standard referred to in the Apple docs.

Resources