- (NSString *) displayInvitationDate:(NSString *)paramDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *date = [dateFormatter dateFromString: paramDate];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd MMM"];
NSString *convertedString = [dateFormatter stringFromDate:date];
return convertedString;
}
That's calling above function
[self displayInvitationDate:#"2016-02-27"];
Oddly result is
Wed 27 JAN
and result should be
SAT 27 JAN
Please let me know which part got wrong above my coding.
You should add this line.
[dateFormatter setTimeZone:[NSTimeZonetimeZoneWithAbbreviation:#"GMT"]];
and chanfe your pass foemat yyyy-mm-dd to replace yyyy-MM-dd, For set as....
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
For more Details Data time Format to Click Now
Related
I want to display the date in the format "28 Mar 2016 / 16:54pm" from the string "2017-05-05T08:27:31.337Z". How can I do this? Is there any buildin method for iOS? Thanks.
use NSDateFormatter for your concept
for e.g
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
NSString *final = [dateFormatter stringFromDate:msgDate];
update
add setAMSymbol and setPMSymbol in your dateFormatter.
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
Full Answer
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
NSString *final = [dateFormatter stringFromDate:msgDate];
See Instruction or introduction with every line
NSString *str = #"your date here..."; /// here this is your date with format yyyyy-MM-dd'T'HH:mm:ss.SSSZ
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);
I want to convert my input string to particular NSDate format,
Here is the code I have tried.
NSString * result;
NSDate * resultDate;
NSDate *date = [dateFormat dateFromString:maxUpcomingDateString];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.locale=[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat1 setDateFormat:#"yyyy/MMM/dd"];
NSLog(#"maxUpcomingDateString %#",maxUpcomingDateString)
NSDate *d=[dateFormat1 dateFromString:maxUpcomingDateString];
result = [dateFormat1 stringFromDate:date];
resultDate=[dateFormat1 dateFromString:result];
NSLog(#"max1 date upcoming %#",result);
NSLog(#"max2 date upcoming %#", resultDate);
Output of my log shows:
maxUpcomingDateString 10 January 2017
max1 date upcoming 2017/Jan/10
max2 date upcoming (null)
I want the output date in the form of 2017/Jan/10. When I try to log the string it shows me the correct output but when I convert it back to NSDate it shows null.
nsstring to nsdate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
nsdate to nsstring
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
add this code on dateFormat
[dateFormat setDateFormat:#"dd MMM yyyy"];
It will solve the problem.
Add this code after allocating the dateFormat object.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM yyyy"];
I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);
I need to take the NSString Dec 4, 2012, 12:33 PM and convert it to separate out the month, day, and year, so that I can have 3 different strings of 12, 04, and 2012.
I figure that I should convert the NSString to NSDate and then reformat the date to change out NSString, but am running into issues.
I have:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:substring];
NSLog(#"Date%#", dateFromString);
[dateFormatter release];
However, the date keeps coming back null.
The problem is with your locale I think.
You should try to print how your dateFormatter formats current date [NSDate new].
It works for me:
NSString* substring = #"Dec 12 2012 12:08 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setDateFormat:#"MMM d yyyy h:mm a"]; // not 'p' but 'a'
NSDate *dateFromString = [dateFormatter dateFromString:substring];
Convert
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm p"];
to, Because MMM dd, yyyy, hh:mm p not a valid date formate
[dateFormatter setDateFormat:#"MMM dd, yyyy, hh:mm a"];
And
NSDate *dateFromString = [dateFormatter dateFromString:substring];
To get string again
NSLog(#"%#",[dateFormatter stringFromDate:dateFromString]);
for me NSLog is Dec 04, 2012, 12:33 PM
#import "NSString+Date.h"
#implementation NSString (Date)
+ (NSDate*)stringDateFromString:(NSString*)string;
{
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
return dateFromString;
}
+(NSString*)StringFromDate :(NSDate*)date;
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
return stringDate;
}
` #end
I'm using the Twitter API to download the number of tweets because I need them in my app for iOS. I'm using this function to do this. The problem is that I need to know the day, month and year when the tweet was written.
The date that comes back is like this:
Thu Oct 25 10:34:58 +0000 2012
But I need a date like this format:
25/10/2012 10:34
So, I want to transform the first format to the second format. I've tried to do it with NSDate and NSDateFormatter, obtaining a NSDate with the first NSString, and after transforming this NSDate in the second NSString, but I didn't get it.
NSArray *dateArray = [tweets valueForKey:#"created_at"];
NSString *dateString = [dateArray objectAtIndex:0]; // Thu Oct 25 10:34:58 +0000 2012
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSString *realDateStr = [dateFormatter stringFromDate:date];
NSLog(#"%#", realDateStr);
The two NSLogs each return null.
Can anybody help me? Thanks.
Try setting the date format originally to parse out the date string you are getting, like this:
NSString *dateString = #"Thu Oct 25 10:34:58 +0000 2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm"];
NSString *realDateStr = [dateFormatter stringFromDate:date];
NSLog(#"%#", realDateStr);
Try this simple method:
NSString* string=#"26-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:string];
NSLog(#"Date = %#",date);