Time format in specific format [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
format a NSDate to DDMMYYYY?
Want NSDateformatter for January,2000 or January 2000
I need date and time in following format. How can I get ..
Dec 17,2012 5:30 AM

You can always google for these kind of issues
Try to look at NSDateFormatter Class,
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy hh:mm:ss a"]; //dateFormatter is an object of class NSDateFormatter

NSDate *selected_Date = //Your Date object here;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd,yyy hh:mm:ss a"];
NSString* strDateObj [NSString stringWithFormat:#"%#",[dateFormat stringFromDate:selected_Date]];
NSLog(#"Date %#",)

Related

NSDateFormat dd/MM/yyyy to MM/dd/yyyy iOS [duplicate]

This question already has answers here:
Changing date format on iOS
(2 answers)
Closed 8 years ago.
Was wondering if there is an easy conversion to take a date in the format dd/MM/yyyy and transform it to MM/dd/yyyy to display.
Current code tried is:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
However, date will return nil in this situation. Any help would be appreciated. Thank you.
You'll have to "decode" and then "encode". You're currently trying to parse 28 as a month. Try this:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter stringFromDate:date];

Getting one date less [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 8 years ago.
I don't why i'm getting one date less, when I'm converting a string from a date, i'm getting one date less, e.g. when i'm converting 18/06/2014, i'm getting 2014-06-17, Any idea why this problem, my codes are:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
This is what I'm getting wholly from the log: 2014-06-17 20:00:00 +0000
You will have to take the timezone into account. Your current timezone seems to be ahead of GMT. If you print the entire date with say a time stamp, then you will get the difference. So i suggest you add the timezone to the NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
NSLog(#"Date : %#", date);

Wrong Conversion [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
NSDateFormatter converting into wrong date don't know why
I am converting following string 19-01-2014 01:06:54 PM into date using following code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"DD-MM-YYYY hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:startTime];
And i am getting following output which is incorrect.Please suggest some thing
Printing description of date:
2014-01-04 07:36:54 +0000
The "DD-MM-YYYY" part in your format string is not correct, is should be "dd-MM-yyyy".
(See http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns for a
full list of all date formats.)
Also you should set a "POSIX locale" to be independent of the user's locale/region
settings:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [dateFormat dateFromString:startTime];
Printing an NSDate will return the default description -- since NSDates don't take locale, timezone, etc. into consideration, it defaults to UTC +/- 0000 (notice the +0000).

NSDateFormatter giving incorrect output with am/ pm [duplicate]

This question already has answers here:
Get NSDate from NSDate adjusted with timezone
(2 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
Below is what I am using
NSString *myDate = #"01-11-2014 10:22 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [dateFormatter dateFromString:myDate];
NSLog(#"new date===%#=====%#", newDate, myDate);
Below is what I am getting
new date===2014-11-01 19:22:00 +0000=====01-11-2014 10:22 PM
^^
Output I was expecting is
new date===2014-11-01 22:22:00 +0000=====01-11-2014 10:22 PM
^^
Any idea what is going wrong?
When I have AM I have below output
new date===2014-11-01 07:22:00 +0000=====01-11-2014 10:22 AM
^^
Edit 1
Actually what I am doing is asked date and time in UITextField (sadly but true as client wanted it in same way)... and then concatenating this string and converting it to NSDate.
So what I have is
NSString myDate = [NSString stringWithFormat#"%# %#", appDate, appTime];
Add this line of code:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
You probably had the wrong timezone. You have to set the timezone to your timezone.

Date formatter for the google calender API [duplicate]

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 9 years ago.
1) I am getting the date from the google calender. The format is 2013-05-03T22:30:00.000+02:00 like this. Which date format used for this type of date in the app.
2) I have to show the date like this format Feb 4th 2013 .
How to implement these two features in the my app? Please help me.
Thank you.
To get the NSDate from that string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS Z"];
NSDate *myDate = [dateFormat dateFromString:#"2013-05-03T22:30:00.000+02:00"];
Once you have your NSDate, you can do the same to get the representation:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:"MMM d yyyy"];
NSString *myString = [myDate stringFromDate:myDate];
You can consult the whole date formatting options here:
Date Formatter Guide
The date is in RFC3339 format. I suggest you to check this gist where you can see a category on NSDate called NSDate (InternetDateTime). This will do the formatting for your input.
For your second question, check the usage of NSDateFormatter.

Resources