I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter 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];
Related
I am getting an value of 2015-04-22 10:43:57 from server but getting problem when converting into NSDate. I'd like to convert the same into NSDate into same format given in NSString. Here is my code
// getting from server
NSString *dateString=#"2015-04-22 10:43:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [dateFormat dateFromString:dateString];
If I will get dateString with timezone then will it be resolved.
Your code looks correct.
I imagine you are having a problem with timezones while formatting. You culd addnit to your NSDateFormatter to specify if input date is from an specific timeZone
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
Please use below code
NSString *dateString=#"2015-04-22 10:43:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
dateFormat.timeZone=localTimeZone;
NSDate *aDate = [dateFormat dateFromString:dateString];
OutPut: 2015-04-22 10:43:57 +0000
Hope it helps you..!
Your code is correct. You just try adding this line to your code.
//This will set time zone of your device.You can change systemTimeZone to localTimeZone based on your need.
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
I have a NSString like #"2014-11-27T10:54:08.185Z"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate * date = [dateFormatter dateFromString:self.creationTime];
I tried a lot of different formatter strings, but the date is always nil.
Or is there another problem here?
The hour in your date string format seems to be in the 24h format.
So you need to use HH instead of hh in your dateFormat.
More info
Try
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.creationTime];
self.dateLabel.text = [formatter stringFromDate:date];
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];
Please try this...:)
I am working with NSDate i created one function which give Today date with "dd/MM/yyyy" format but NSDate always show "nil" value. I googled lot and also see lots of stackoverflow answer but all answer return NSString value not NSDate value so please guide me to get NSDate with given format.
+(NSDate*)getCurrentDate{
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
//Date print here
NSLog(#"Utility CurrentDate: %#",[DateFormatter stringFromDate:[NSDate date]]);
NSString *dateString = [DateFormatter stringFromDate:[NSDate date]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:DATE_FORMAT];
//Date print here
NSLog(#"New Date: %#",[newDateFormatter stringFromDate:[NSDate date]]);
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
return dateFromString;
}
The issue is here
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
Here your dateString is 2014-12-11 03:41:31, hence it is not getting formatted. You should change either newDateFormatter or DateFormatter to convert as per your requirement.
EDIT:
Replace [DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; with [DateFormatter setDateFormat: DATE_FORMAT];
But your return date will not be in the desired way, since you are returning an NSDate Object. You should return a formatted string.
You are completely misunderstanding what NSDate is and does. An NSDate is an absolute point in time. It has no format. It cannot have a format. It is absolutely impossible to have an NSDate in "dd/MM/yyyy" format or in any other format.
When you want to display a date to the user, you use NSDateFormatter to convert the NSDate into a string. In any format you like.
check here Convert date from string or you can use this methods to convert.
These all three functions you can use to format date values..
-(NSString*)getStringFromDate:(NSDate*)pDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter stringFromDate:pDate];}
-(NSDate*)getDateFromString:(NSString*)pStrDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter dateFromString:pStrDate];}
-(NSString*)dateStringFromString:(NSString *)dateToConver format:(NSString *)fromFormat toFormat:(NSString *)toFormat{
NSDate *date = [Helper getDateFromString:dateToConver withFormat:fromFormat];
return [Helper getStringFromDate:date withFormat:toFormat];}
Refer the below modified method in dd/MM/yyyy format:-
+(NSString*)getCurrentDate{
NSDateFormatter *format=[[NSDateFormatter alloc]init];
[format setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *currentDt=[format stringFromDate:[NSDate date]];
NSDate *dt=[format dateFromString:currentDt];
[format setDateFormat:#"dd/MM/yyy"];
return [format stringFromDate:dt];
}
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);
I have made a calendar application for the iPhone in which I have a date in string format (e.g. "Tue, 25 May 2010 12:53:58 +0000").
I want to convert this to an NSDate.
What do I need to use to do that?
Take a look at the class reference for NSDateFormatter. You use it like this:
NSString *dateStr = #"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
For more information on how to customize that NSDateFormatter, try this reference guide.
EDIT:
Just so you know, this is going to parse the full month name. If you want three letter month names, use LLL instead of LLLL.
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
NSString *finalDate = #"2014-10-15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"EE, d MMM, YYYY"];
return [dateFormatter stringFromDate:date];
}
If you are storing dates in one of iOS' styles, it's far easier and less error prone to use this method:
// Define a date formatter for storage, full style for more flexibility
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
// Use today as an example
NSDate *today = [NSDate date];
// Format to a string using predefined styles
NSString *dateString = [dateFormatter stringFromDate:today];
// Format back to a date using the same styles
NSDate *todayFromString = [dateFormatter dateFromString:dateString];
NSString *dateString=#"2017-05-25";
NSDateFormatter *dateFormatter=[NSDateFormatter alloc]init];
[dateFormatter setDateFormatter:#"MM-dd-yyyy"];
NSDate *date =[[NSDate alloc]init];
date=[datFormatter dateFromString:dateString];