How do you get the seconds since a past date to now? - nsdate

How can I get the seconds that have passed since 1980-01-01 00:00:00 +1100 using NSTimeInterval?
// I need the function to use something like and am having an issue
NSDate *aDate = (NSDate*)#"1980-01-01 00:00:00 +1100";
NSDate *seconds = [NSDate dateWithTimeInterval:60*60*24 sinceDate:aDate];
NSLog(#"seconds since Jan 1980 %#",seconds);
// I am trying to replace the following
//NSTimeInterval dateinterval = [[NSDate date] timeIntervalSince1970];
NSTimeInterval dateinterval = seconds;
NSDate only retrieves the GMT at +0000 which is not helpful in real world applications. Local dates are mandatory.
Is this too hard or can it not be done this way?

You are initializing your NSDate object wrong. You can't directly cast an NSString as an NSDate, you need to use NSDateFormatter:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss z";
NSDate *aDate = [formatter dateFromString:#"1980-01-01 00:00:00 +1100"];
NSDate *now = [NSDate date];
NSTimeInterval seconds = [now timeIntervalSinceDate:aDate];
To format the date for a different time zone, use a new NSDateFormatter and set its local and timeZone.

Related

NSDate rolling year over to next year on Dec 31

If I have this date:
dtToUse __NSTaggedDate * 2017-12-31 05:00:00 UTC
which I get from a string using this method:
- (NSDate*) convertStringToDate : (NSString*) strToConvert andTheFormatToUse: (NSString*) strFormat {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strFormat];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
return [dateFormatter dateFromString:strToConvert];
}
and I use this code to display it:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
NSString* strDate = [dateFormatter stringFromDate:dtExpiration];
self.txtCurrentField.text = [dateFormatter stringFromDate:dtExpiration];
But no matter what I do, the year increments 1 - so 2017 becomes 2018
This only happens for Dec. 31, any year. I suspect it is a time zone issue but I am not sure how to fix, as I am converting time zone to local already
Just try
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
instead of
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
After that, it will show 12/31/2017.
Different between YYYY and yyyy.
https://stackoverflow.com/a/15133656/1342266

String is not converting into date according to time zone

I am receiving date in string form from server and I need to show the date according to my time zone (Indian: GMT +5:30).
Here is my code
NSString *dateString = #"2015-08-10 11:45:10";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);
//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(#"date = %#", localDateString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSDate *date2 = [dateFormatter2 dateFromString:localDateString];
NSLog(#"%#",date);
And the Log is:
2015-08-10 11:45:10 +0000 (NSDate)
2015-08-10 11:45:10 (NSString)
date = 2015-08-10 17:15:10 (NSString)
2015-08-10 11:45:10 +0000 (NSDate)
My issue is with last log (2015-08-10 11:45:10 +0000)
Why it is not 2015-08-10 17:15:10??
The last line prints what it does because you're printing an NSDate, and NSDate does not have a time zone. It literally has no attribute or internal state that indicates a time zone. Time zones exist only for presenting data to users, commonly as you're doing via NSDateFormatter. But if you log the value of an NSDate you'll get a GMT string, since the NSDate itself has no time zone.

Formatting Date from server time to local time using NSTimeZone and Dateformatter

I am receiving a date/time as a NSString from my server where I am converting that time into a NSDate to the users local time using NSTimeZone. After which I try to reformat this NSDate into a better more readable NSString using the new NSDateFormatter format, however when I try to apply this new format it reverts the resulting dateString back to the original Server time.
I would like to know what I am doing wrong, I would like to show the converted time in the new format.
this is the code I am using
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
// change time to systemTimeZone
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSDate *localTime = [dateFormatter dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [dateFormat1 stringFromDate:localTime];
NSLog(#"TimeZone - %#", timeZone);
NSLog(#"UTC ServerTime - %#", [singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]);
NSLog(#"UTC to deviceTimeZone - %#", localTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);
This is an example of my output
TimeZone - Pacific/Auckland (NZST) offset 43200
UTC ServerTime - 2013-08-22 01:45:59
UTC to deviceTimeZone - 2013-08-21 13:45:59 +0000
NewFormat - 22/08/13 - 01:45 AM
any help would be greatly appreciated
The NSDateFormatter that reads the date must be set to the timezone that the date you are parsing is in, in your case, it is UTC. The date formatter will then be able to produce an NSDate object (which represents a specific moment in time regardless of timezones). You can then give that NSDate object to another NSDateFormatter that is configured to format dates in a specific time zone.
// set date format
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = #"yyyy-MM-dd HH:mm:ss";
dateParser.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSDate *specificMomentInTime = [dateParser dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yy - hh:mm a";
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:specificMomentInTime];
NSLog(#"UTC ServerTime - %#", specificMomentInTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);

how to get only date from string?

how to get only date from datetime date value
my code is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
lblDateAvailable.text = [dateFormat stringFromDate:[datepick date]];
NSDate *date=[dateFormat dateFromString:lblDateAvailable.text];
NSLog(#"%#",date);
result=2012-08-17 18:30:00 +0000
i want to get only date from string.so plz suggest me.
Convert Date into string and using this method Separated the component and pass the Separated value in your label.
NSArray* components = [yourdatestring componentsSeparatedByString:#" "];

NSDateFormatter gives different output/wrong (GMT?) time

I have tried setting the timezone and locale of the NSDateFormatter but I can't seem to get anything to work. Here is the code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
NSString *myFireDateString=[#"9/17/11" stringByAppendingString:#" 09:00:00"];
NSDate *myFireDate = [dateFormat dateFromString:myFireDateString];
NSLog(#"The datestring is is %#",myFireDateString);
NSLog(#"The formatted date is %#",myFireDate);
Here is the output:
The datestring is is 9/17/11 09:00:00
The formatted date is 2011-09-17 13:00:00
You need to set a timezone to your dateFormatter:
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];
See NSDate from NSString , shows the previous day

Resources