Converting string with 12 hour time format into NSDate - ios

I need to change string that is in 12 hour time format into NSDate. I am using below given code but its returning null. Can anyone help me with this??
NSString = #"04/03/2013 7pm";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSDate *date = [df dateFromString:selectedDateString];

It is giving you null as your DateFormat does not match to your string.
Try this,
NSString *selectedDateString= #"04/03/2013 7pm";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy ha"];
NSDate *date = [df dateFromString:selectedDateString];

Related

Converting a NSString to NSDate

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];

Which dateFormat should I choose while converting NSString to NSDate?

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...:)

NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format

My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate?
I tried:
NSString *currentDateString = #"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS+05:30"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
It returns nil. Any thoughts?
Use this code it is working fine your dateFormatter was wrong .
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
Happy coding!!!!!!!!!!.
Your date format contains a time zone as a literal: yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 the +05:30 it the time zone definition. You should parse it with Z.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSString *currentDateString = #"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
NSString *p=#"2014-01-08T21:21:22.737+05:30";
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *datefor=[dateformat dateFromString:p];
[dateformat setDateFormat:#"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
I think you need to initialize the dateFormatter like this below in your code. Because if do not initialze always you will get the null value:-
dateFormatter=[[NSDateFormatter alloc]init];
Set your dateFormat as
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
And Yes, Now NSLog is Printing a specific Date.!!!
Enjoy Coding

Converting string to date and finding time difference

I want to convert a string to a date time object and then subtract the difference between 2 dates. However I keep getting an invalid pointer warning. The date is formatted 20131209 02:34. My code is below
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"YYYYMMDD HH:MM"];
NSDate *currTime = [df dateFromString:self.currentTime];
NSDate *predicTime =[df dateFromString:self.predictedTime];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];
Your date format was incorrect [df setDateFormat:#"YYYYMMDD HH:MM"]; I assumed that your strings are valid !
Try :
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd HH:mm"]; // 24hr format, for 12hr format use 'hh'
NSDate *currTime = [df dateFromString:#"20131209 02:34"];
NSDate *predicTime =[df dateFromString:#"20131209 04:34"];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];
try out this code
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd hh:mm"];
NSDate *currTime = [df dateFromString:self.currentTime];
NSDate *predicTime =[df dateFromString:self.predictedTime];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];

Troubles with parsing ISO8601 time in iOS

I'm trying to convert this string "2011-11-23T17:59:00Z" to an NSDate.
I've seen many people have this problem, but everyone has a slightly different format. I haven't been able to hack a solution.
I've tried code such as:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.timeStyle = NSDateFormatterFullStyle;
[dateFormat setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString* date = ""2011-11-23T17:59:00Z"";
NSString *dateString = [dateFormat stringFromDate: date];
dateString always comes back NULL
NSDateFormatter stringFromDate takes an NSDate object and you're passing it a string.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Try NSDateFormatter dateFromString.
If you use dateFromString:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: #"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString* dateStr = #"2011-11-23T17:59:00Z";
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"date: %#", date);
Outputs:
2011-12-14 00:34:57.587 Craplet[440:707] date: 2011-11-23 22:59:00 +0000

Resources