Converting the time in datetime format in iPhone - iphone

To all:
Can someone please help me about how to convert the time (HH:mm) format to the datetime(yyyy/mm/dd HH:mm:ss) format? I need help.
I already tried this String to a Format yyyy-MM-dd HH:mm:ss Iphone link, but I am having the string in this format HH:mm and I want to convert this into the yyyy/mm/dd HH:mm:ss
Thanks in advance!!

#Neelz saying right If in DB you are saving only hours & minute value then it will be depends on you which date you are using to get the formatted date.I did the same which #Neelz said but in different way.Below I shown the code by considering the today's date to get the date time as you need
//get the today's date
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:#"en_US"]];
//Set the required date format to get the todays date
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString* cCurrentDateStamp = [formatter stringFromDate:date];
NSLog(#"%#",cCurrentDateStamp);
//now get the hours & minute you saved in DB.I considered 11:08
NSString *strYOurTime=#"11:08";
//create the date time string by appending date & time
NSString *str=[NSString stringWithFormat:#"%# %#:00",cCurrentDateStamp,strYOurTime];
//set the formatter
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *FinalDateTime=[formatter dateFromString:str];
NSLog(#"%#",FinalDateTime);
FinalDateTime is the final date which you want.Now to get the number of days between two dates refer following link:
iPhone - get number of days between two dates

Related

Date conversion issue while setting region from settings application

I want to convert string to date. Its not working in following situation.
I am using settings application to set region. In settings, go to general, go to international, go to region format, go to spanish and then set Argentina.
After setting these parameter, I try to convert string to date. Its not converting string to date. Please help.
EDIT : This is my code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
NSDate *_date = [formatter dateFromString:strDate];
This document explains what you're experiencing
In short, format your date with the american locale, if you wish to display the date to a user, convert the date to a string.
For example:
NSString *strDate = #"05/29/2012 6:15PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSDate *_date = [formatter dateFromString:strDate];
And to show the date to a user:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *newDate = [formatter2 stringFromDate:_date];
Date strings entering the application should typically be converted to a standard format, should you ever want to display a date to the user, you would need to format the date back to a string - think of them as seperate steps:
Get the string to a sane date format
Get the date to the users locale with the desired formatting
When you are converting a String to Date, the String must be in Spanish(Argentina) format for the NSDateFormatter to understand.
in your example, the date you passed is "05/29/2012 6:15PM"
However, in Spanish(Argentina), the date string should be "05/29/2012 6:15p.m."
If you are getting this date dynamically, best is to remove the AM/PM component in the date and try getting the Date String in 24-Hour format

Convert NSString to NSDate returns (null) value

I have one Date in string format "2013-03-19T19:00:50"
I am trying to convert it into NSDate using NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddThh:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(#"date in date format : %#",startDate);
but it is giving me null date
date in date format : (null)
What is the issue?
Use :
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
You have time in 24 hour format, so HH is required. hh is used when time is in 12 hour format.
And T is required to be in single quote, T is not a part of date this is an added text on it.
According to the date formatter patterns hh means Hour [1-12] You want Hour [0-23] which is HH.
And any letters that are not date format patterns, or must not be interpreted in this way have to be put in between apostrophes.
use [dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
Do like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *startDate = [dateFormatter dateFromString:date];
NSLog(#"date in date format : %#",startDate);

Converting NSString to nsdate not working

I am converting date from NSString to NSDate using following code
NSString *dateString = #"julho-29-2012 05:01 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
NSDate* dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#",dateFromString);
[dateFormatter release];
But it not working for me :(
That's because the formats don't match.
"julho-29-2012 05:01 PM"
Follows the following format:
"MMMM-dd-yyyy hh:mm a"
Basically your date formatter isn't able to understand your string and gives a default value, nil.
Edit
You might need to change the formatters locale since your date strings are not in English but Portuguese.
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"pt-PT"]];
Your date format string doesn't match the date format in the string.
Try:
[dateFormatter setDateFormat:#"MMMM-dd-yyyy hh:mm a"];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:#"pt"]];
Setting the locale should only be needed if your phone is not in Portuguese already.
your date formatter's format does not match ur date in string format.

how to get correct time from Uidatepicker

I am using below code to get date and time from datepicker.
When i am using formatter it is giving correct time .
but when i am converting that string into NSdate object ,then it is not giving the correct value of time.
NSDate *choice = [datepicker date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSString *dateString = [format stringFromDate:choice];
NSDate *date = [format dateFromString:dateString]; NSLog(#"%#",date);
DateString is giving correct value but when i am converting it in to NSdate again it is giving wrong value.
If the data that is being logged has some hour in difference then its ok since logging an NSDate will log it in GMT time zone, then this is not a bug, if your dateString is correct and your NSdate has some hours of difference then you have no issues

Can I use NSDateFormatter to convert this date string to an NSDate?

I have this string...
2010-08-24T16:00:00-05:00
and I'd like to extract the time portion from it (i.e. 16:00) and convert it to its 12-hour equivalent (i.e. 04:00 pm). I'm trying to use NSDateFormatter to accomplish this, but it's not working...
NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"hh:mm a"];
NSDate *date1 = [dateformatter dateFromString:[listOfTimes objectAtIndex:0]];
[dateformatter release];
Can I use NSDateFormatter with this date format? If not, how can I extract the time and convert it to its 12-hour time equivalent?
Thanks!
The problem has to do with parsing the colon. I asked the same question and the solution is here: How to parse a date string into an NSDate object in iOS?
I think you should be able to do something like the following.
// create the date formatter object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate* date = [formatter dateFromString:dateString];
// set up the new date format
[formatter setDateFormat:#"hh:mm:ss"];
NSString *twelveHourTime = [formatter stringFromDate:date];
[formatter release];
Update: Fixed the dateFormatter string format. I had the line below, but the Z seems to be unnecessary. Timezones always screw me up. :-/
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
This answer needs to be updated. As of iOS 10 the system provided NSISO8601DateFormatter is available for this particular format.