Problems with changing the date & time format in objective c xcode - objective-c

I've been at this for a while, browsing all the forums for help, but I just can't get this to work. I'm new to xcode and i'm trying to change yyyy-mm-dd hh:mm:ss +0000 into e.g 21st March 2014, 6:30pm. My current code is:
-(void)viewDidLoad{
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:#selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[_postTime setInputView:datePicker];
}
-(void)updateTextField:(id)sender{
if([_postTime isFirstResponder]){
UIDatePicker *picker = (UIDatePicker*)_postTime.inputView;
_postTime.text = [NSString stringWithFormat:#"%#",picker.date];
}
}
I'd appreciate all the help I can get.
Thanks

Use NSDateFormatter with a dateFormat string. If you want that yyyy-mm-dd format, use yyyy-MM-dd HH:mm:ss Z (note the capital MM for month and HH for hour):
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z";
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
_postTime.text = [formatter stringFromDate:picker.date];
If you want that 21st March 2014, 6:30pm format in your local timezone, you can use something like:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"d MMMM yyyy, hh:mma";
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US"];
_postTime.text = [formatter stringFromDate:picker.date];
If you want to format that date in the format specified by the user's device (which is a nice way to present date/time, respectful of the user's preferences in settings):
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateStyle = NSDateFormatterLongStyle;
formatter.timeStyle = NSDateFormatterShortStyle;
_postTime.text = [formatter stringFromDate:picker.date];
Refer to the NSDateFormatter Class Reference or the Date Formatters section of the Data Formatting Guide for more information. The date formatter gives you a great deal of control over how you want the date formatted.

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

UIDatePicker Order Format

I am new to ios development tried UIDatepicker format like day month date. I tried alot but not able to get, Can any one help ?
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewController.selectedDate"];
storedDate = [dateFormat dateFromString:#"EE,MMMM dd"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.pickerView setDate:storedDate animated:NO];
i need the above image format
First, get the saved date in string Format.
NSString *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewControllerselectedDate"];
Then get the format in which the date is stored so that you can convert it to date.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
Now you can convert the stored-date to date format and set in datepicker.
NSDate *sortedDateFormatted = [format dateFromString:storedDate];
[self.pickerView setDate:sortedDateFormatted animated:NO];
You need the date format ryt, Just try this
NSDate *now = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
NSString * dateStr = [[format stringFromDate:now] capitalizedString];
NSDate *sortedDateFormatted = [format dateFromString:dateStr];
[self.pickerView setDate:sortedDateFormatted animated:NO];

Convert ISO 8601 to NSDate

I have a timestamp coming from server that looks like this:
2013-04-18T08:49:58.157+0000
I've tried removing the colons, I've tried all of these:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
Why NSDateFormatter can not parse date from ISO 8601 format
Here is where I am at:
+ (NSDate *)dateUsingStringFromAPI:(NSString *)dateString {
NSDateFormatter *dateFormatter;
dateFormatter = [[NSDateFormatter alloc] init];
//#"yyyy-MM-dd'T'HH:mm:ss'Z'" - doesn't work
//#"yyyy-MM-dd'T'HH:mm:ssZZZ" - doesn't work
//#"yyyy-MM-dd'T'HH:mm:sss" - doesn't work
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// NSDateFormatter does not like ISO 8601 so strip the milliseconds and timezone
dateString = [dateString substringWithRange:NSMakeRange(0, [dateString length]-5)];
return [dateFormatter dateFromString:dateString];
}
One of my biggest questions is, is the date format I have above really ISO 8601? All the examples I have seen from people the formats of each are slightly different. Some have ...157-0000, others don't have anything at the end.
This works for me:
NSString *dateString = #"2013-04-18T08:49:58.157+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
// Always use this locale when parsing fixed format date strings
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:posix];
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"date = %#", date);
There is New API from Apple! NSISO8601DateFormatter
NSString *dateSTR = #"2005-06-27T21:00:00Z";
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
NSDate *date = [formatter dateFromString:dateSTR];
NSLog(#"%#", date);
I also have the native API, which is way cleaner... This is the implementation I got in my DateTimeManager class:
+ (NSDate *)getDateFromISO8601:(NSString *)strDate{
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
NSDate *date = [formatter dateFromString: strDate];
return date;
}
Just copy and paste the method, it would do the trick. Enjoy it!
The perfect and best solution that worked for me is:
let isoFormatter = ISO8601DateFormatter();
isoFormatter.formatOptions = [ISO8601DateFormatter.Options.withColonSeparatorInTime,
ISO8601DateFormatter.Options.withFractionalSeconds,
ISO8601DateFormatter.Options.withFullDate,
ISO8601DateFormatter.Options.withFullTime,
ISO8601DateFormatter.Options.withTimeZone]
let date = isoFormatter.date(from: dateStr);
For further more detail, you can refer to apple's official documentation: https://developer.apple.com/documentation/foundation/nsiso8601dateformatter

dateFromString returns the wrong date

I have the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-mm-dd";
NSDate *cDate = [formatter dateFromString:thisLine];
NSLog(#"cDate '%#' thisLine '%#'", cDate, thisLine);
NSLog prints:
cDate '2011-01-10 05:07:00 +0000' thisLine '2011-07-10'
while cDate should be '2011-07-10'
Any thoughts?
Thanks
Lowercase mm is for minutes not months, month use uppercase MM:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *cDate = [formatter dateFromString:thisLine];
NSLog(#"cDate '%#' thisLine '%#'", cDate, thisLine);
The NSDate description will always print the date with its own formatting, generally for the +000 time zone. You need to use the date format to get the correctly formatted date and use MM for month not mm.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *cDate = [formatter dateFromString:thisLine];
NSLog(#"cDate '%#' thisLine '%#'", [formatter stringFromDate:cDate], thisLine);
-(NSString*)description
Discussion The representation is not guaranteed to remain
constant across different releases of the operating system. To format
a date, you should use a date formatter object instead (see
NSDateFormatter and Data Formatting Guide)

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)