I have the following piece of code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
I am getting the NSLog statement as Parsed Date.. 2011-12-25 00:00:00 +0000. I would appreciate any help in resolving this. I just want to convert that NSString to its NSDate equivalent. Tried using NSTimeZone as well, but no effect. What am I missing here?
You need to use lower case year indication, yyyy instead of YYYY:
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
If you want to get the timezone right you need to set the timezone of the NSDateFormatter. By default it uses the timezone of your device. For the time at GMT use the following.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
You are using an incorrect format string. See the documentation. Basically, capital YYYY is "week of year year". Try to use lowercase yyyy instead, i.e.
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
Related
I am getting the following string to be used, but it is from UTC-0, and I need to convert it to CST.
serverdate = # "2017-07-31 02:18:50";
I added the following code, but it returns nil
NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:#"yyyy-MM-dd HH:mm"];
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"CST"]];
NSString *time =[dfLocal stringFromDate:serverdate];
NSLog(#"%#", time);
I even tried the following options, no luck.. still it returns nil.
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"CDT"]];
and tried
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"America/Chicago"]];
First you need to handle the seconds in your time:
[dfLocal setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Next set the timezone to UTC and convert your string:
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *serverUTC = [dfLocal dateFromString:serverdate];
Now change the time zone and convert back to a string:
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"America/Chicago"]];
NSString *time =[dfLocal stringFromDate:serverUTC];
Use America/Chicago as that will handle DST correctly.
HTH
Try this answer:
NSString *strInputDateString = #"2017-07-31 02:18:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:#"dd-MM-YYYY hh:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CST"]];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(#"%#",strInputDateString);
NSLog(#"%#",strOutputDateString);
A few thoughts:
You actually need to do two separate things here; first, you need to parse the UTC date string you've received from the server into a date, and second, you need to display that date as a string in CST.
Your dateFormat does indeed need to include the :ss at the end, as Steven mentioned.
If there's not a specific reason that you need CST, but rather the problem is something like this being an internal app for a company that happens to be located in the Central time zone area, then I'd suggest using [NSTimeZone defaultTimeZone] instead. This allows your app to keep on Just Working™ if somebody suddenly needs to use it somewhere else, and will also automatically handle things like daylight savings.
With all that said, here's some code that should do what you want:
NSString *serverdate = #"2017-07-31 02:18:50";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [formatter dateFromString:serverdate];
// [NSTimeZone timeZoneWithName:#"CST"] should work too
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *time = [formatter stringFromDate:date];
NSLog(#"%#", time);
I think, you should change your dateFormat from yyyy-MM-dd HH:mm to yyyy-MM-dd HH:mm:ss.
I am trying to convert date with specific formate and just wanted to get date from passed date as shown below but getting nil
What am I doing wrong?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *date = [dateFormatter dateFromString: [dicValues valueForKey:#"DateNeeded"]];
[dateFormatter setDateFormat:#"dd/MM/yyyy h:mm a"];
NSString *strSubTitle = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:date]];
Date i am receiving is : 2016-11-17T23:46:50.35 , 2016-11-28T21:25:20.927 and 2016-11-29T00:00:00
You need to convert
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
to this
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
As in your date string you are receiving MilliSeconds also
Edit
This code is working for me, check the date format which you are getting from server
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormatter dateFromString: #"2016-11-28T21:25:20.927"];
if (date == nil) {
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
date = [dateFormatter dateFromString: #"2016-11-28T21:25:20.927"];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy h:mm a"];
NSString *strSubTitle = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:date]];
[dicValues valueForKey: #"DateNeeded"] is either nil or does not match the specified format "yyyy-MM-dd HH:mm:ss".
First check that date you receive, is in yyyy-MM-dd HH:mm:ss format. If not then make this format according to date you received.
For Example, you received date 29/11/16, then your code will be like this
[dateFormatter setDateFormat:#"dd/MM/yy"];
NSDate *date = [dateFormatter dateFromString: [dicValues valueForKey:#"DateNeeded"]];
Once you get date, then convert it any format you want.
try
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
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];
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 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];
}