I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);
Related
I am getting the following string to be used, but it is from UTC-0, and I need to convert it to CST.
serverdate = # "2017-07-31 02:18:50";
I added the following code, but it returns nil
NSDateFormatter *dfLocal = [[NSDateFormatter alloc] init];
[dfLocal setDateFormat:#"yyyy-MM-dd HH:mm"];
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"CST"]];
NSString *time =[dfLocal stringFromDate:serverdate];
NSLog(#"%#", time);
I even tried the following options, no luck.. still it returns nil.
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"CDT"]];
and tried
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"America/Chicago"]];
First you need to handle the seconds in your time:
[dfLocal setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Next set the timezone to UTC and convert your string:
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *serverUTC = [dfLocal dateFromString:serverdate];
Now change the time zone and convert back to a string:
[dfLocal setTimeZone:[NSTimeZone timeZoneWithName:#"America/Chicago"]];
NSString *time =[dfLocal stringFromDate:serverUTC];
Use America/Chicago as that will handle DST correctly.
HTH
Try this answer:
NSString *strInputDateString = #"2017-07-31 02:18:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:#"dd-MM-YYYY hh:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CST"]];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(#"%#",strInputDateString);
NSLog(#"%#",strOutputDateString);
A few thoughts:
You actually need to do two separate things here; first, you need to parse the UTC date string you've received from the server into a date, and second, you need to display that date as a string in CST.
Your dateFormat does indeed need to include the :ss at the end, as Steven mentioned.
If there's not a specific reason that you need CST, but rather the problem is something like this being an internal app for a company that happens to be located in the Central time zone area, then I'd suggest using [NSTimeZone defaultTimeZone] instead. This allows your app to keep on Just Working™ if somebody suddenly needs to use it somewhere else, and will also automatically handle things like daylight savings.
With all that said, here's some code that should do what you want:
NSString *serverdate = #"2017-07-31 02:18:50";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [formatter dateFromString:serverdate];
// [NSTimeZone timeZoneWithName:#"CST"] should work too
[formatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *time = [formatter stringFromDate:date];
NSLog(#"%#", time);
I think, you should change your dateFormat from yyyy-MM-dd HH:mm to yyyy-MM-dd HH:mm:ss.
I have the need to store both local time as per users timezone and UTC time in a transaction table using core data
What I want is as follows
createDate should reflect the users local timezone, in this case I am in China so GMT+8
createDateUtc should represent the same timestamp but in UTC
I have tried various formatters but for some reason I cannot get the result I want
Here are my two formatters as you can see I tried using timezone as well but it still did not set the date to UTC.
NSDateFormatter *dateFormatterWithTz = [[NSDateFormatter alloc] init];
[dateFormatterWithTz setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDateFormatter *dateFormatterUtc = [[NSDateFormatter alloc] init];
[dateFormatterUtc setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
//[dateFormatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
here is how I create the date variables
NSDate *createdDateUtc = [NSDate date];
NSDate *createdDate = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:createdDateUtc];
Here is what the array looks like which is incorrect as you can see it created my Utc date as my local date and pushed the created date to tomorrow and still using the same time offset, the issue should be that my UTC date is set to local time but I cannot figure out how to make it UTC so that my created date gets calculated correctly from that
createdDate = "2016-12-25T03:19:38+08:00";
createdDateUtc = "2016-12-24T19:19:38+08:00";
you need to provide timeZone for date formater
for local timeZone try this
NSDateFormatter *formate =[[NSDateFormatter alloc] init];
[formate setTimeZone:[NSTimeZone localTimeZone]];
[formate setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSString *strToday =[formate stringFromDate:[NSDate date]];
and For UTC timezone set UTC timezone like this
NSDateFormatter *formate =[[NSDateFormatter alloc] init];
[formate setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formate setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSString *strToday =[formate stringFromDate:[NSDate date]];
hope this will help you.
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.
Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.
This is the code I'm using to create the date from string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
and my date string is #"2015-08-22 13:00:00"
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:#"2015-08-22 13:00:00"];
return date;
The last line returns nil.
You need to change:
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
With:
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Since hh refers to the 12 hour clock and HH refers to the 24 hour clock.
hh represents hours in 12-hour-mode and 13 is out of range.
Replace hh with HH for 24-hour-mode
I had exactly the same problem last week and I solved it with this code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateFromString.doubleValue];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate = [dateFormatter stringFromDate:date];
Here you have a string named dateFromString because this is the object type for date which I recieve from server. This string is converted in double value and the date is calculated with time interval.
I suppose you need this date to be shown in a label or text view or another UI element like this, and just because of that I converted the date into NSString to be easy to use it. This is what I have in stringDate.
Let me know if I have to help you more.
smaller case letters for 12 hour clock and upper case for 24-hr. Change hh with HH.
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];