So i am getting a string containing date and time in this format "2014-12-22T11:00:00+0500" Now in order to convert it into NSdate i am using
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate* date = [dateFormatter dateFromString:start_time];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString* temp = [dateFormatter stringFromDate:date];
self.eventDate = [dateFormatter dateFromString:temp];
NSDateFormatter* timeFormatter = [[NSDateFormatter alloc]init];
[timeFormatter setDateFormat:#"HH:mm:ss"];
NSString* temp2 = [timeFormatter stringFromDate:date];
self.start_time = [timeFormatter dateFromString:temp2];
Now even though the conversion is successful the problem is that eventDate also has has time after date 00:00:00. How can i remove this so that eventDate only contains date.
Conversly start_time has the time of event but also has some arbritrary reference date before that. How can i remove that so i only have time in start_time
I have searched hard and fast but haven't been able to figure out this problem. Any help would be appreciated.
You cannot remove either the date or the time to keep only one component. If I remember correctly NSDate object is internally just a number of seconds relative to a fixed point in time. So every NSDate contains the full date and time information.
What you probably want to do is to get the NSDateComponents you want from a NSDate object.
Instead of trying to store this separate, just display these dates separate. I think it could be useful sometimes to get the date completly, but i don't know your idea.
You can try with it, it may be help you.
NSString *finalDate = #"2014-12-22T11:00:00+0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dateFormatter dateFromString:finalDate];
//For getting Time
NSDateFormatter* df1 = [[NSDateFormatter alloc]init];
[df1 setDateFormat:#"hh:mm:ss"];
NSString *time = [df1 stringFromDate:date];
NSLog(#"time %# ", time);
//For getting Date
NSDateFormatter* df2 = [[NSDateFormatter alloc]init];
[df2 setDateFormat:#"yyyy-MM-dd"];
NSString *actualDate = [df2 stringFromDate:date];
NSLog(#"actualDate %# ", actualDate);
Related
I am storing list of data in sqlite for my iOS app. In DB one column 'addedOn' stores dates from server response in format '30-Jul-2014 07:43:20'.
Here I have tried many things on 'addedOn' column but could not fetch date in ASC/DESC order.
Some other details are
Column 'addedOn' date type is DATETIME.
Queries tried to fetch in DESC used are:
"SELECT * FROM tableName ORDER BY datetime(addedOn) DESC LIMIT 1"
& "SELECT * FROM tableName ORDER BY date(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY addedOn DESC LIMIT 1".
But all these queries failed to give me expected result.
I tried to convert this date in another format while inserting in DB it self but converting of this date format in other date format like 'yyyy-MM-dd HH:mm:ss' isn't working at all. For that tried below code:
NSString *dateString = #"30-Jul-2014 07:43:20";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSString *stringDate = [dateFormatter stringFromDate:dateFromString];
NSLog(#"stringDate %#", stringDate);
So my question is this possible to convert this type format in any other format or is there any thing else that I am missing. Please share your thought and suggest me the changes that I need to do. Any help is appreciated.
Your dataFormat seems to be wrong. Try the following DateFormat.
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:yourDateString];
This will give you exact date.
Hope it helps..
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"creationDate" ascending:YES];
try NSPredict and use the above code
I have my DB with SQLite & have date in format "dd-mmm-yyyy HH:mm:ss"
in which the below query works..
SELECT * FROM TableName ORDER BY addedOn ASC
ASC|DESC can be used as per requirement..
And if you want to convert your date format than do as under :
NSString * dateStr = #"30-Jul-2014 07:43:20";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"dd-MMM-yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *finalString = [dateFormat stringFromDate:date];
here the output is finalString = "2014-07-30 07:43:20"
Thanks folks for your time and suggesting me changes that I need to do.
Storing of NSDate as it is in DB instead of Converting NSDate in NSString and then inserting in DB solved my problem with little change in code
There are two cases in which my code works now
NSString *dateString = MD_history.addedOn;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MMM-yyyy HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [df dateFromString:dateString];
And
NSString *dateString = MD_history.addedOn;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[df setLocale:usLocale];
[df setDateFormat:#"dd-MMM-yyyy HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [df dateFromString:dateString];
In this case Date after inserting in DB is in "2014-07-30 02:13:20 +0000" format. In this sorting of date column works proper.
I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;
// getting today's date string
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
[dateFormatterToday setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];
// converting today's date string to NSDATE
NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
[dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm a"];
dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];
Here currentDateTimeString, which is in string format showing my current time as :
2016-01-11 17:52 PM (which is correct one)
but dateTodayFinal, which is in Date format shows:
2016-01-11 07:22:00 +0000
I have tested with different timezones, but the issue persist, please help some one. Thank you.
The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.
Add:
NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(#"dateTodayFinalTimeString: %#", dateTodayFinalTimeString);
Output:
dateTodayFinalTimeString: 2016-01-11 07:57 AM
NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.
If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];
// converting today's date string to NSDATE
//
// NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
// [dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];
Also have a look at the docs.
I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
It looks easy, but I couldn't figure out a proper way to do this. I need to create an NSString from a NSDate which represents the same time on every device, independently from the iPhone's time zone settings.
Suppose userA is in London, where the actual time is 14:00, userB is in New York where is 9:00 and userC is in Hong Kong, where the actual time is 21:00.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
Actually with this code I'm getting these results (when I log the dateString):
userA: 08/12/14 14:00:00
userB: 08/12/14 09:00:00
userC: 08/12/14 21:00:00
But I need to create dates like this
userA: 08/12/14 14:00:00
userB: 08/12/14 14:00:00
userC: 08/12/14 14:00:00
My goal is to create a "system/absolute time" which is the same inside the app and doesn't matter the original time zone of the user's device. The end result must look like this MM/dd/yy HH:mm:s.
Is it possible to get the NSDate *now = [[NSDate alloc] init]; from a pre-defined timezone? For example it could always use the actual time of the GMT-00 timezone.
I've tried to do it with this code, but when I run the code, the console writes out the wrong date (based on the device time zone setting) again, so I don't have a better idea. I would appreciate any ideas.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"Europe/London"];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"the date is %#,", dateString);
The below code should work. What ever the timezone you are in it will always display the time in UTC.
NSDate *now = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"UTC"]];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:now];
NSLog(#"%#",dateString);
Between the following two threads, I think you'll find everything you need (and thensome). The first is an extensive example of a problem similar to yours (just remember to look at the code in the answers and not the question), while the second has all the time zone abbreviations that you'll ever need. Gotta love the helpful people on The Stack.
The links again, just in case
objective-c: Conversion between TimeZones
GMT timezone conversion in objective c
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
timeFormatter.dateFormat = #"HH:mm:ss";
NSString *dateString = [timeFormatter stringFromDate: localDate];
This is really odd, the code below takes self.danceTimeIn (its text state) and converts it to an actual time. The problem is that its coming up 1 hour LESS than what's entered. Meaning that if I enter 14:03 I'll get 13:03 in the database! The same thing is happening with the date version of this code
++++++++++++++++++++++
TIME
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
DATE
NSString *danceDateValue = self.danceDate.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yy"];
NSDate *date = [dateFormat dateFromString: danceDateValue];
++++++++++++++++++++++
Anyone ???
Try using this. NSDateFormater change date according to locale of your device settings, if you set locale properlyl, you will get proper date. Try if this works :)
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatIn setLocale:usLocale];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
Time zone may be causing this problem.Try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
May be this will help.
It depends on the timezone:-
first Check your local time zone
NSTimeZone *tz=[NSTimeZone timeZoneWithName:#"GMT"];
[dateformat setTimeZone:tz];
and then set your date accordingly.