This question already has an answer here:
NSDateFormatter Issues
(1 answer)
Closed 8 years ago.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-YYYY"];
NSDate *myTime = [[NSDate alloc] init];
myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
I am converting a stored date(currently hardcoded) into NSDate format so that I can display it and apply operations on it. But myTime is giving wrong output.
2013-12-21 18:30:00 +0000
whereas I am passing 07-22-2014 as string and expecting me to output 07-22-2014.
Do I need some formatting apart from setting it right. ? What is the reason of going wrong.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate* myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
try this
-(NSString *)dateFormater:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd hh:mm:ss";
NSString *strDate=[formatter stringFromDate:date];
formatter = nil;
return strDate;
}
use like this..
NSString *temp=[self dateFormater:[NSDate date]];
NSLog(#"%#",temp);
Related
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"];
This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 6 years ago.
I am receiving TimeString from server in such format - "2012-04-23T14:00:00.511Z" and for some reason I get nil when trying to convert that into NSDate object by doing following
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:00.511Z"];
What might be the issue?
Your code should be like,
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:51Z"];
NSLog(#"%#",myDate);
I have seen the other answer and question but i didn't find any useful answer for me.
I have a string like 2014-10-07T07:29:55.850Z and I want to have a date to compare each other.
If I use this code:
NSString *fullDate = payload[#"sent_ts"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:fullDate];
My date is nil. Why?
Try This.
NSString *dateString = #"2014-10-07T07:29:55.850Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
This question already has answers here:
NSDate from NSString gives null result
(3 answers)
Closed 9 years ago.
My code is the following:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);//null
How to fix that? thanx.
It is because you are using unix timestamp and you can convert unix timestamp into year or any date format as :
//Posted Date Format
NSString *dateStr = #"1339007317";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy"]; //Here you can set any date format Ex:#"dd MMM, yyyy hh:mm a" or#"dd/MM/yyyyy" according to your requirement
[dateFormatter1 setLocale:locale];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatter1 stringFromDate: date];
Hope it helps you.
You need two date formats. The first should match the format of the string you wish to convert to an NSDate. The second format needs to represent the format you want. Use the 2nd to convert the NSDate to the new string.
Also, why do you create a date object then immediately replace it with a new one?
Change this:
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
to:
NSDate *date = [dateFormatter dateFromString:dateString];
Your code needs to be like this:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???"]; // format that matches the dateString
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"yyyy"]; // the desired new format
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I make a request to the flickr api and it returns me the date in the following format
"2013-02-01T06:25:47Z"
How do i convert this into NSDate format??
It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
You can use below function:
-(NSDate *)getDateFromString:(NSString *)pstrDate
{
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dtPostDate = [df1 dateFromString:pstrDate];
return dtPostDate;
}
Hope, it will be helpful to you.
This function will accept your string date and return the NSDate value.
Cheers!