I am trying to transform a datestring into another format. I am doing it like this.
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/YYYY"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
But this is what I get from my NSLOGs
2013-04-18 08:43:36.181 mosaqua2[9629:907] datestring is NOW 2013-05-04
2013-04-18 08:43:36.184 mosaqua2[9629:907] date transformed 2013-01-03 23:00:00 +0000
2013-04-18 08:43:36.184 mosaqua2[9629:907] datestring is AFTER donderdag, 03/01/2013
What I want it to be is
Saterday, 04/05/2013
Can anybody help me ?
Use my this bellow custom method for convert Date Format ...
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
and use it like bellow...
NSString *strSDate = [self changeDateFormat:_dayObject.p_date dateFormat:#"yyyy-MM-dd" getwithFormat:#"EEEE, dd/MM/yyyy"];
NSLog(#"datestring is AFTER %#",strSDate);
See My Blog with this post...
Check this.....
NSLog(#"datestring is NOW ==>> %#", _dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *date = [dateFormat dateFromString: _dayObject.p_date];
NSLog(#"date transformed ==>> %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER ==>> %#",dateStr);
OUTPUT:~
datestring is NOW ==>> 2013-05-04
date transformed ==>> 2013-05-04 00:00:00 +0000
datestring is AFTER ==>> Saturday, 04/05/2013
Check the character case of parameters
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //MODIFIED
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/yyyy"]; //MODIFIED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
Please try ..this it will work to get the week name
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"eeee"];
NSCalendar * cal = [NSCalendar currentCalendar];
NSUInteger numWeekdays = [cal maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
NSDateComponents * comp = [[NSDateComponents alloc] init];
for( NSUInteger day = 1; day <= numWeekdays; day++ ){
[comp setWeekday:day];
[comp setWeek:0];
NSDate * date = [cal dateFromComponents:comp];
NSString * dayName = [dateFormat stringFromDate:date];
NSLog(#"%#", dayName);
}
You are doing all right except setTimeZone , comment that you will get your output.
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/YYYY"];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]]; //NOT NEEDED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
Change this : "YYYY"
To : "yyyy"
Reason for the error,
It's a common mistake to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar. In most cases, yyyy and YYYY yield the same
number, however they may be different. Typically you should use the
calendar year.
Related
I have a date string like this "2013-09-05T06:40:19Z" from server
i want to convert it into date which format should be same as my iphone follow .
how to do this ?
when i do this ?
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:time];
NSLog(#"%#",date)//getting null here
i am getting null in date.
NSString *dateStr = #"2013-09-05T06:40:19Z";
NSString *outDateFormate = #"MMM dd yyyy";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *intime = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:outDateFormate];
NSString *dateWithNewFormat = [dateFormat stringFromDate:intime];
Since you are getting the date in specified format so you need to define your date formatter accordingly
NSString *gameDateString = #"2013-09-05T06:40:19Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *gameDate = [dateFormatter dateFromString:gameDateString];
NSLog#("** The date is %#",gameDate);
// Now convert date to string in the format which you required.
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *dateString = [dateFormatter stringFromDate:gameDate];
NSLog#("** The date string is %#",dateString);
Hope this helps.
NSString *str=#"2013-09-05T06:40:19Z";
str=[[str stringByReplacingOccurrencesOfString:#"T" withString:#" "]stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:str];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
NSString *newDate=[dateFormatter1 stringFromDate:date];
NSLog(#"%#",newDate)
Below is my code
NSString *dateandtime =[NSString stringWithFormat:#"%#",self.eventtime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateandtime];
if i print my self.event name i am getting the output as Mon, 7 Jan 2013 15:30:00 CST but when i pass this string in NSDateFormatter i am getting null value i don't know why i tried many combinations of date formats, but i cant solve it, can anyone help me
i solved my problem as Per Prateek comments, now its working for me...Thanks Prateek.
in your self.event, you are getting CST, it should be time zone for example +0530
Use :
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
Use this code...
- (NSString *)formattedDate:(NSString*)dateSte {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateSte];
[dateFormatter setDateFormat:#"EEE,MMM d,''yyyy"];
NSString *strDate = [dateFormatter stringFromDate:date];
return strDate;
}
Use the below method for getting the NSDate from NSString date
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];
Paste this below method in your .m file...
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here format in which the date should be
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
For More Information About NSDate component see this post of My Blog NSDate-formate-info-for-iphone-sdk
I am struggling a bit to convert the UTC time to the date format what i want.
2012-03-10T09:30:00Z
needs to be converted to the below format
3:00:00pm April 19, 2012
Could you someone please help me to resolve this?
I tried like below,
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
// Here datetime= 2012-03-10T09:30:00Z
NSDate *date = [NSDate dateWithTimeIntervalSince1970: [datetime intValue]];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
But, its printing wrong date like "06:03:32 Thursday, Jan 1". Please correct me.
UPDATED: I am having another problem, its not giving me the correct time, but its giving the correct date though. My Code is below.
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:datetime];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Thank you!
To convert an ISO date string to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:date_str];
You can then convert it to your local time zone:
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:#"HH:mm:ss EEEE, MMM d"];
In your code, your [datetime intValue] is probably incorrect.
I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
This is my code,
NSString *dateString = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Converted date is %#",dateFromString);
Output is,
Converted date is (null)
Yuva.M
NSString *dateWithInitialFormat = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, yyyy"];
NSDate* d = [df dateFromString:#"January 06, 1992"];
NSLog(#"%#", d);
NSDate *dispalyDate = d;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM"];
NSString *str = [dateFormat stringFromDate:dispalyDate];
NSLog(#"%#", str);
I have the following code:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
[df dateFromString:#"2011-04-12 10:00:00"];
In which it always generates a null date. Why is this?
If the device is set to AM/PM time and requested string format is set to #"yyyy-MM-dd HH:mm:ss" dateFromString will return nil.
If you set the locale to #"en_US" conversion will return correct date.
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:#"2013-02-14 09:30:00"];
Are you doing this in a background thread? I had weird experiences with NSDateFormatter when not being used in the ui-thread. Anyway, here's the method I use, should work for you:
+ (NSDate*)parseDate:(NSString*)inStrDate format:(NSString*)inFormat {
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setLocale:[NSLocale systemLocale]];
[dtFormatter setDateFormat:inFormat];
NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
[dtFormatter release];
return dateOutput;
}
I had the same issue. I started by double-checking that the string used for conversion was valid by using this resource.
I then had a thought that it might be the locale, so I tried the following:
[dateFomatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_AU"]]
It worked like a charm. Hope this helps.
I just ran this code and it worked for me.
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *theDate = [df dateFromString:#"2011-04-12 10:00:00"];
NSLog(#"date: %#", theDate);
The output was: date: 2011-04-12 17:00:00 +0000
Yes, agreed with #Jan Gressmann, this is so wierd! I am using the NSDateFormatter on non-ui thread as well, and dateFromString selector returns null sometimes... For example, I have this code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale:usLocale];
[dateFormat setDateFormat:#"dd MMM yy HH:mm:ss"];
NSDate *startDate = [dateFormat dateFromString:str_StartDate];
NSDate *endDate = [dateFormat dateFromString:str_endDate];
Output in console:
(lldb) po str_StartDate
(NSString *) $7 = 0x06da2180 03 SEP 2012 10:00:00
(lldb) po str_endDate
(NSString *) $8 = 0x06d3a810 08 SEP 2013 10:00:00
(lldb) po startDate
(NSDate *) $9 = 0x00000000 <nil>
(lldb) po endDate
(NSDate *) $10 = 0x06db05b0 2013-09-08 02:00:00 +0000
This is really wierd...
I was getting similar issue with iOS8 (XCode 6), I did following changes in NSDateFormatter -
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *formattedDate = [dateFormatter dateFromString:dateString];
dateFromString and stringFromDate return null because of device time format (ex: 12hr Or 24hr) and device time zone
Convert 12hr To 24hr
- (NSString*)getTimeIn24Hr:(NSString*)time12hrWithDate
{
NSDateFormatter* df12 = [[NSDateFormatter alloc] init];
[df12 setTimeZone:[NSTimeZone systemTimeZone]];
[df12 setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate* dateTime12hr = [df12 dateFromString:time12hrWithDate];
NSDateFormatter *df24 = [[NSDateFormatter alloc]init];
[df24 setLocale:[NSLocale systemLocale]];
[df24 setDateFormat:#"HH:mm:ss"];
NSString * time24HrOutput = [df24 stringFromDate:dateTime12hr];
return time24HrOutput;
}
Convert 24hr To 12hr
- (NSString*)getTimeIn12Hr:(NSString*)time24hrWithDate
{
NSDateFormatter* df24 = [[NSDateFormatter alloc] init];
[df24 setLocale:[NSLocale systemLocale]];
[df24 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* dateTime24hr = [df24 dateFromString:time24hrWithDate];
NSDateFormatter *df12 = [[NSDateFormatter alloc]init];
[df12 setDateFormat:#"hh:mm a"];
NSString * time12HrOutput = [df12 stringFromDate:dateTime24hr];
return time12HrOutput;
}
I have the similar issue with you, dateFromString: success in ios simulator, while in iPhone, it always return NULL, after I add the below code attached with dateFormatter, it success in iPhone ad simulator:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
Try to set the both date formatters in the same format while converting (date to string) and (string to date).In the following format
[formatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"]
[formatter2 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"]
You can use this format "yyyy-MM-dd hh:mm:ss".
The key is using downcased hh rather than HH to format hour when you use swift2.3 on iOS10
Hope that could be useful.