NSDateFormatter produces null result Objective-C - objective-c

So I have this string date
"2017-07-25T11:02:00.000Z"
I want to format this date so that I can assign it to my date picker
Below is my code
//Set the date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:self.userDefaultQuotationModel.dateTime];
[self.quotationDatePicker setDate:dateFromString];
however, if I set the date format as #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ", it gives me the correct result based on the format. But afterwards, I can't assign this date to the date picker.
I set the date format as #"MMM d, yyyy" because I am looking for the the nearest format as Date Picker's
my Date picker format is Jul 25 2017. I get the date format from this following website
http://nsdateformatter.com/
The *dateFromString does not contain any value after the dateFromString function, which is nil
I have been searching regarding this topic but couldn't find any. What am I missing here? thank you

NSString *strInputDateString = #"2017-07-25T11:02:00.000Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:#"MMM d, yyyy"];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(#"%#",strInputDateString);
NSLog(#"%#",strOutputDateString);

Related

How to format date 12/2/31 H, 2:21:41 AM in Objective-c?

This is what date I am getting again our current date of 2019.
12/2/31 H, 2:21:41 AM
Can anyone help me how can I parse this date to NSDate object in Objective-c?
This calendar format belongs to the Japanese Calendar.
To parse the date with NSDateFormatter you have to replace the H with Heisei and assign a Japanese Calendar instance to the date formatter.
NSString *dateString = [#"12/2/31 H, 2:21:41 AM" stringByReplacingOccurrencesOfString:#"H" withString:#"Heisei"];
NSCalendar *japanese = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US#calendar=japanese"];
formatter.calendar = japanese;
formatter.dateFormat = #"MM/d/yy G, h:mm:ss a";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"%#", date);

objective c convert string to UTC- the time is moving in error

I return a string from my database and value is '04/27/2016 1:16pm'. This value is already in UTC.
Now I want to convert that string to NSDATE, keeping it in UTC. When I try to convert string to date, the time is actually moving by 1 hour.
This is how I am doing it
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
//date conversions
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
the result is coming back as 2016-04-27 12:16:01 UTC.
result is
2016-04-27 10:12:44.612 x[1558:48608] tuploadtime=4/27/2016 2:10:33 PM, duploadtime=2016-04-27 12:10:33 +0000
basically the time is moving back 1 hour but I want to keep it the same.
hope I am making sense
Proper String Format for Date is most Important.
There are some methods to have HOURS format as follow with their differences..
kk: will return 24 format Hour in (01-24) hours will (look like 01, 02..24).
HH will return 24 format Hour in (00-23) hours will(look like 00, 01..23).
hh will return 12 format Hour (look like 01, 02..12).
so you should use your code like
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
for More Date format refer this link
Your date format string is inconsistent:
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
HH means to use a 24-hour format. But then you also used a to indicate AM/PM. Using both is confusing the formatter and giving you off-by-one. You meant to use hh here.

String to Date and Time formate and date and time to string in objectiveC

I tried to convert String to date time and date time to string it's working but some what error. I not getting correct formate.
my excepting result should be:
like this.02/02/2013 05:24PM
time should be 12 hours formate;
NSString *dateTime = #"2013-02-02 15:54:31";
int time=[[temp objectForKey:#"created"]intValue];
NSLog(#"Time=%d",time);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:dateTime]];
NSDate *date = [dateFormat dateFromString:dateTime];
NSLog(#"Nsdate=%#",date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"hh:mma"];
dateTime = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"DateTime=%#",dateTime);
--------------------
NSLogs :
Time=2013
DateTime=2013-02-02 17:06:09
Nsdate=2013-01-02 11:30:00 +0000//it's not correct date
DateTime=02/01/2013 05:00PM//it's not correct date and time//
-------------
my excepting result should be like this:
DateTime :02/02/2013 05:24PM
If you have any idea please shear your ideas , It's will help me.
Change your this line
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
to
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:SS"];
mm and dd should be small
Hope it helps you..

Create a date in xcode label

First of all I want to say thanks in advance for helping me.
I want to know how can I create a date on a label using Xcode,
and the date will follow the same date like in iphone.
Thanks
Here's one simple approach
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//uncomment to get the time only
//[formatter setDateFormat:#"hh:mm a"];
//[formatter setDateFormat:#"MMM dd, YYYY"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
//get the date today
NSString *dateToday = [formatter stringFromDate:[NSDate date]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
[label setText:dateToday];
[formatter release];
//then add to a view
Creating the text date is simple, but the real question is what is your data source that you want to make the date text out of?
The date can be formatted and set in label as below.
NSDate *today = [NSDate date];
NSDateFormatter *dformat = [[NSDateFormatter alloc] init];
[dformat setDateFormat:#"dd:MM:YYYY"];
myLabel.text = [dformat stringFromDate:today];
NSDateFormatter handle format of string dates.
Instances of NSDateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects.
Try this and see:
// Set date format according to your string date format
// e.g.: For,
// 22-12-1996 -> #"dd-MM-yyyy"
// 22/12/1996 -> #"dd/MM/yyyy"
// 1996-12-22 03:45:20 -> #"yyyy-MM-dd HH:mm:ss"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yyyy";
//dateFormatter.dateFormat = #"dd-MM-yyyy";
NSDate *date = [dateFormatter dateFromString:dateString];
if(date == nil) {
correctFormat = false;
}
NSLog("Date: %#",date);
Note: Each pairs of characters in date format relates relevant date component with date instance. You can create any type of date format using date string pattern.
Here is document by Apple: Date Formatters
Date (Day): dd
Month: MM or MMM or MMMM
Year: yy or yyyy
Here is list of date formats: Date Formats
Here is solution in Swift
var today = Date()
var d_format = DateFormatter()
d_format.dateFormat = "dd:MM:yyyy"
label.text = dformat.string(from: today)

NSDateFormatter returns 1970 instead of current year with format MM/dd

The following code:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"MM/dd"];
NSDate *newTime = [dateFormatter dateFromString:#"10/01"];
sets the resulting date to 10/01/1970. Is there a "right" way to have the formatter assume that you mean a date in the current year, or closest to the present date?
Did you try [dateFormatter setLenient:YES]?