IOS: NSDateFormatter - ios

I am trying to create something similar to Kevin Lawler's Timeago for future dates. It seems that using dateFormatter and setting SetDoesRelativeDateFormatting:YES is the closest native api to that library or similar libraries in other languages. Apple provides the sample code below that I have working. However, its future capability is limited to swapping tomorrow for tomorrow's date and so I am trying to combine it with NSDateFormatter. For some reason, however, I can't get the two to work together.
Apple reference code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.doesRelativeDateFormatting = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString); //logs valid output
If instead of using timestyle or datestyle, however, I use setDateFormat when I log out the result, I get nil.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
dateFormatter.doesRelativeDateFormatting = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString); //logs blank
Does doesRelativeDateFormatting only work with NSDateFormatterMediumStyle or can you combine it with setDateFormat in some way that I am not seeing?

Please use below code it worked for me
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.doesRelativeDateFormatting = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString); //logs valid output
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
dateFormatter.doesRelativeDateFormatting = NO;
date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString); //logs date correctly

Related

Right dateFormat for ISO 8601 date

I have stacked with dateFormat of 2017-12-13T10:30:00.000-06:00 for NSDateFormatter. I'd tried different formats but still getting nil from [formatter dateFromString:exampleDate]. The most possible format I had picked up on nsdateformatter was #"yyyy-MM-dd'T'HH:mm:ss.SSSZ" but TimeZone not applying correctly and I got nil from NSDateFormatter.
I also try to use:
NSISO8601DateFormatter *dateFormatter = [[NSISO8601DateFormatter alloc] init];
[dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
and also got nil.
You can use NSISO8601DateFormatter with options .withFractionalSeconds (iOS 11.0+) and .withInternetDateTime
NSISO8601DateFormatter *dateFormatter = [[NSISO8601DateFormatter alloc] init];
dateFormatter.formatOptions = NSISO8601DateFormatWithFractionalSeconds | NSISO8601DateFormatWithInternetDateTime;
NSDate *date = [dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
NSLog(#"%#", date);
For a standard NSDateFormatter the date format is correct
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSDate *date = [dateFormatter dateFromString:#"2017-12-13T10:30:00.000-06:00"];
NSLog(#"%#", date);

How to format date using NSDateFormatter iOS?

I tried to use the NSDateFormatter to format a date that selected from a UIDatepicker. But the formatted result giving a nil.
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MMM-dd HH:mm:ss";
NSDate *formattedDate = [dateFormatter dateFromString:date];
return [dateFormatter stringFromDate:formattedDate];
}
date input is 2016-04-22 16:30:36 +0000(after selected from UIDatePicker). How may I fix this?
As mentioned in the comment you can get the NSDate directly from the UIDatePicker and pass that date directly in the formatter
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:datePicker.date];//pass the date you get from UIDatePicker
If you want to convert the same way you have mentioned in the question you can try like this:
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate * convrtedDate = [formatter dateFromString:date];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:convrtedDate];
return dateString;
}

Convert date from weird string to date in format day month and year

Hi I am parsing an end point of API, from there I am getting a string which contain like this date string 2016-01-18T13:28:06.357 but I want to convert this like 01 Jan 16, I have tried many times by using different format one of following mentioned. kindly need suggestion how to accomplish this task.
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateformat date];
// Objective C
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *myDate = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSString *strDate=[dateformat stringFromDate:myDate];
// Swift 3.0
var date = (dictionary["date"] as! String)
var dateformat = DateFormatter()
dateformat.timeZone = NSTimeZone(name: "EST")
dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
var myDate = dateformat.date(fromString: date)!
dateformat.dateFormat = "dd MMM yyyy"
var strDate = dateformat.string(from: myDate)
You should use this format:
NSString *dateStr = #"2016-01-18T13:28:06.357";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [formatter dateFromString:dateStr];
// date is 2016-01-18 08:28:06 +0000
And next convert that date to format you need
You can also use this :
NSString *dateString = #"2016-01-18T13:28:06.357";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd MMMM yy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"newDateString=%#",newDateString);
You should use this format code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Get Date
[formatter setDateFormat:#"dd"];
NSString *strDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date is - %#",strDate);
//Get Month
[formatter setDateFormat:#"MMM"];
NSString *strMonth = [formatter stringFromDate:[NSDate date]];
NSLog(#"Month is - %#",strMonth);
//Get Year
[formatter setDateFormat:#"yyyy"];
NSString *strYear = [formatter stringFromDate:[NSDate date]];
NSLog(#"Year is - %#",strYear);
Log is :
Date is - 05
Month is - Feb
Year is - 2016
for set more format, Click Me

NSDateFormatter for day and month name

I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);

IOS date returns null

I have date in object "start_time"(2000-12-11T00:00:00+0000), but i need to print date and time separate to textfields. But when i tried with this code i got null value on console
__block NSDictionary *events = [[e.response objectForKey:#"events"] objectAtIndex:0];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSDate *dates = [events objectForKey:#"start_time"];
// NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"MMMM dd yyyy";
NSString *dateString = [dateFormatter stringFromDate: [events objectForKey:#"start_time"]];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] init];
timeFormatter.dateFormat = #"HH:mm:ss ZZZZ";
NSString *timeString = [timeFormatter stringFromDate: [events objectForKey:#"start_time"]];
NSLog(#"This is time------------------------------: %#",dateString); // null returns
//
This is how i store date into "start_time" by textfields
Blockquote
NSString *datestring = self.date.text;
NSString *timestring = self.time.text;
NSString *combined = [NSString stringWithFormat:#"%# %#", datestring, timestring];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:05.30]];
[dateFormat setDateFormat:#"MM dd yyyy HH:mm a"];
NSDate *date = [dateFormat dateFromString:combined];
NSDictionary *data = #{
#"start_time":[NSString stringWithFormat:#"%#",date]};
I think that became null Because you upcoming date from event that is different format and you are first setting format is different.
dateFormatter.dateFormat = #"MMMM dd yyyy";
that have to:
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
You have to set first current date format then you can change it your want date.
Your setting an incorrect format for the date,
Try the below, this will work
//"myDate" variable is an example string, you can change it with "[events objectForKey:#"start_time"]"
NSString *myDate = #"2000-12-11T00:00:00 +0000";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"yyyy-dd-mm'T'HH:mm:ss Z";
NSDate *myDateObject = [dateFormatter dateFromString:myDate]; //Now you have the date object that return the correct value
//FOR date
[dateFormatter setDateFormat:#"yyyy-dd-mm"];
NSString *OnlyDate = [dateFormatter stringFromDate:myDateObject];
//For Time
[dateFormatter setDateFormat:#"HH:mm:ss Z"];
NSString *OnlyTime = [dateFormatter stringFromDate:myDateObject];
NSLog(#"Date is %# and Time is %#",OnlyDate,OnlyTime);
UPDATE
In your case, if [events objectForKey:#"start_time"] is a NSDATE object then try the following
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] init];
dateFormatter.dateFormat = #"yyyy-dd-mm'T'HH:mm:ss Z";
NSDate *myDateObject = [dateFormatter dateFromString:[events objectForKey:#"start_time"]]; //Now you have the date object that return the correct value
//FOR date
[dateFormatter setDateFormat:#"yyyy-dd-mm"];
NSString *OnlyDate = [dateFormatter stringFromDate:myDateObject];
//For Time
[dateFormatter setDateFormat:#"HH:mm:ss Z"];
NSString *OnlyTime = [dateFormatter stringFromDate:myDateObject];
NSLog(#"Date is %# and Time is %#",OnlyDate,OnlyTime);
Hope this helps.

Resources