I have a date like this : Mon, 04 Jun 2012 14:13:00 GMT
and want to delete the time displayed so that I can have something like this : Mon, 04 Jun 2012
I tried the code below :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
NSLog(#"the object value is:%#",dateTex);
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"EEE, dd MMM yyyy"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
The date is parsed from an xml file and saved in the NSString dateTex. Then I am trying to change the format and save the new date on NSString dateText. However when I try to print the dateText variable, I get the object value is (null).
Any ideas?
You can try below code :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
NSLog(#"the object value is:%#",dateTex);
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy"];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
NSString *my_date_string = [dateFormatter stringFromDate:date];
//my_date_string returns like value Tue 05 jun 2012
Welcome!
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 have tried this:
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"%#", myDate);
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy HH:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"%#", myDate);
Your convert date nil because you didn't use right dateFormat
try :
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss"];
more infomation
For your required Format,Use this:-
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:myDateString];
dateFormatter.dateFormat = #"dd-MM-yyyy HH:mm:ss";
NSString *myDate = [dateFormatter stringFromDate:date];
NSLog(#"%#",myDate);
NSString *myDateString = #"29 Feb 2016 20:33:09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy HH:mm:ss"];
NSDate *myDate = [dateFormatter dateFromString:myDateString];
//here i get correct date ok But
NSLog(#"%#", myDate);
NSString *ss=[NSString stringWithFormat:#"%#",myDate] ;
NSArray *components = [ss componentsSeparatedByString: #"+"];
NSString *StartnewDateString = [components objectAtIndex:0];
//Here time is changed of StartnewDateString
I've a date like this (retrieved via Twitter API) Thu Jun 05 12:57:52 +0000 2014 and I would like transform in Italian style: Giovedì 5 Giugno 2014.
Is possible using NSDateFormatter, if yes how ?
Yes it is, use the correct format and locale.
NSString *localeIdentifier = #"en_US";
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"MMMM dd yyyy"];
[dateFormatter setLocale:locale];
NSString *formattedDateString = [dateFormatter stringFromDate:[NSDate date]];
The result from this code will be June 05 2014. As you see I am using en_US locale.
Date Format Patterns
that creates the italian date for you:
NSString *_dateInString = #"Thu Jun 05 12:57:52 +0000 2014";
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_GB"]];
[_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss Z yyyy"];
NSDate *_date = [_dateFormatter dateFromString:_dateInString];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"it_IT"]];
[_dateFormatter setDateFormat:#"EEEE d MMMM yyyy"];
NSLog(#"%#", [_dateFormatter stringFromDate:_date]);
the console says:
giovedì 5 giugno 2014
additionally, if you'd like to change the months' names or the weekdays' names (e.g. making them uppercase), you can play with the following method of the NSDateFormatter (I don't speak any Italian, so I leave it for someone who does):
-setWeekdaySymbols:
-setShortWeekdaySymbols:
-setVeryShortWeekdaySymbols:
-setStandaloneWeekdaySymbols:
-setShortStandaloneWeekdaySymbols:
-setVeryShortStandaloneWeekdaySymbols:
-setMonthSymbols:
-setShortMonthSymbols:
-setVeryShortMonthSymbols:
-setStandaloneMonthSymbols:
-setShortStandaloneMonthSymbols:
-setVeryShortStandaloneMonthSymbols:
you can find the official documetation for those here.
Try this code snippet first i have converted given date to english date then to Italian
NSString * yourJSONString = #"Thu Jun 05 12:57:52 +0000 2014";
NSString *localeIdentifier = #"en_US";
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"EEEE MMMM dd HH:mm:ss Z yyyy"];
[dateFormatter setLocale:locale];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
NSLog(#"%#", dateFromString);
NSString *localeIdentifier1 = #"it_IT";
NSLocale *locale1 = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier1];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];;
[dateFormatter1 setDateFormat:#"EEEE d MMMM yyyy"];
[dateFormatter1 setLocale:locale1];
NSString *formattedDateString = [dateFormatter1 stringFromDate:dateFromString];
NSLog(#"%#", formattedDateString);
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
So i am parsing an xml file and display on screen the dates that i parse.
The dates are like : Thu , 31 May 2012 20:43:54 GMT.
How can i convert the date to this format : dd/mm/yy ?
i dont need the time.
Well it crashes.
I tried :
NSString *date = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:date];
NSString *dateText = [[NSString alloc] initWithString:[dateFormatter stringFromDate:myDate]];
Any ideas?
You can use NSDateFormatter
For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:yourString];
See http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns for dateformat patterns
The idea is to use NSDateFormatter to get a date from your input time string, then use a new dateformatter to get whatever new time string you want from the date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
Alternatively for the second part You can use NSDateComponents.
after:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
//Get NSComponents from date object..
[NSString stringWithFormat:#"%#/%#/%#",dateComponent.date,dateComponent.month,dateComponent.year];