Date with local timestamp - ios

I would like to keep track of the date with timestamp. What objective-C/cocoa class could I use to get the timestamp local to the machine's time settings?
if for example I'm in USA, timestamp should be in terms of the area where I'm at. If I'm in Europe, should be in Europe time.

Take a look at NSDate. For example,
NSDate *today = [NSDate date];
will give you the current date and time.

You can use this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *currentTime = [dateFormatter stringFromDate:[NSDate date]]
NSLog(#"Current Date:%#",currentTime);
I hope this is use full to you.

Related

Convert UTC to CST in NSDateFormatter

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.

Converting calendar date to the correct date

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

Change current time to Indian standard time

I am getting current date by using this
NSDate *currDate;
currDate = [NSDate date];
But If user opens my app from other country also the currDate should be Indian time only.
How can I do this?
Check if this helps, there could be multiple ways of approaching this - but this is readable -
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Asia/Kolkata"]];
NSString *indianTimeZone = [dateFormatter stringFromDate:currentDate];
NSLog(#"%#", indianTimeZone);
I learned about "Asia/Kolkata" by logging -
NSLog(#"%#", [NSTimeZone knownTimeZoneNames]);
Also, checkout this answer, I like this approach.

Storing date and time separately in NSDate

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

Creating absolute time from NSDate

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

Resources