NSDateFormatter show midnight as 24 instead of 00 - objective-c

in my application I use NSDate and NSDateformater to show the hour in the following format : HH:mm. When I pass a timestamp that represents midnight for example: 1603843200000
I expect to get the hour as 00:00, instead I get 24:00. How can I get the hour as 00:00?
Here's the code I use to format the timestamp:
NSNumber* timeStamp = currentConsumption.timestamp; // 1603843200000
NSDate* date = [NSDate dateWithTimeIntervalSince1970:([timeStamp longLongValue]/1000)];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = self->graphDateFormat; // #"k:mm"
if(self->graphType == day){
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
}
return [df stringFromDate:date];

The problem was that I set the format to be k:mm instead HH:mm.

Related

Converting timestamp into NSDate

I have a date in timestamp which looks something like this: 1474914600000
Now, I want to covert this timestamp to NSDate in format of dd-mm-yyyy.
How can this be done in objective c?
You need to convert your timestamp to NSDate and then get NSDate in your desired format. Also your timestamp seems to be in millisecond so you will have to divide it be 1000. You can use below code:
double timeStamp = 1474914600000;
NSTimeInterval timeInterval=timeStamp/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
dateString value as an output will be: "27-09-2016"
Hope it helps.
To elaborate on balkaran's answer incase you're new to the iOS world. The timestamp you provided seems to go down to milliseconds which you wouldn't need for day times that's why he's dividing by 1000. You would use the dateformatter as follows to return an NSString you can use with the formatted date.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1474914600000];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"dd-MM-yyyy";
NSString *formattedDate = [formatter stringFromDate:date];
use this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
then convert your date in the format you want.

objective c convert string to UTC- the time is moving in error

I return a string from my database and value is '04/27/2016 1:16pm'. This value is already in UTC.
Now I want to convert that string to NSDATE, keeping it in UTC. When I try to convert string to date, the time is actually moving by 1 hour.
This is how I am doing it
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
//date conversions
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
the result is coming back as 2016-04-27 12:16:01 UTC.
result is
2016-04-27 10:12:44.612 x[1558:48608] tuploadtime=4/27/2016 2:10:33 PM, duploadtime=2016-04-27 12:10:33 +0000
basically the time is moving back 1 hour but I want to keep it the same.
hope I am making sense
Proper String Format for Date is most Important.
There are some methods to have HOURS format as follow with their differences..
kk: will return 24 format Hour in (01-24) hours will (look like 01, 02..24).
HH will return 24 format Hour in (00-23) hours will(look like 00, 01..23).
hh will return 12 format Hour (look like 01, 02..12).
so you should use your code like
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
for More Date format refer this link
Your date format string is inconsistent:
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
HH means to use a 24-hour format. But then you also used a to indicate AM/PM. Using both is confusing the formatter and giving you off-by-one. You meant to use hh here.

Converting Date and time into number of seconds since 1970

I have the date string "2016-04-01T05:00:00+08:00" and I want to convert it into number of seconds since 1 January 1970. I am doing like this:
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [parsingFormatter dateFromString:"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
But in the above example startTime returns nil. Can someone tell me what I am doing wrong? How can I convert it?
Try this
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [parsingFormatter dateFromString:#"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
I am getting date "2016-03-31 21:00:00 +0000" UTC format.

NSDate is wrong when doing 'dateFromString'

Im here in the UK and when working with dates in iOS they are always out by one hour (one hour behind), what I need is the correct time from an NSDate. Ive done the following, but i get two different times:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"NSDate %#",today);
NSLog(#"Time %#", [dateFormatter stringFromDate:today]);
NSDate*stringDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:today]];
NSLog(#"Time date %#",stringDate);
Here is what is logged:
NSDate 2015-07-01 16:07:22 +0000
Time 2015-07-01 17:07:22
Time date 2015-07-01 16:07:22 +0000
Why is this happening? Am i missing something obvious? Surely if the string date is correct, then doing dateFromString should yield the correct results?
The reason I need an NSDate is so I can get the correct amount of seconds using [myTime timeIntervalSince1970]
2015-07-01 17:07:22 //is the correct date
Im expecting an NSDate object that is correct to my date and time.
Update
The answers below helped me find where I was going wrong, so I changed my approach, I was able to get the current timestamp doing the following:
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSDate *curdate = [dateFormatter dateFromString:timeStamp];
int unix_timestamp = [curdate timeIntervalSince1970];
NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone systemTimeZone];
int offset = (int)[timeZone secondsFromGMTForDate: referenceDate];
int currentTimestamp = unix_timestamp + offset;
NSLog(#"CUrrent time stamp %d",currentTimestamp);
NSDate is an absolute moment of time, it does not have a timezone. The date object you have is correct: it is exactly the moment that code was executed.
If you need string representation of that moment of time in a specific time zone, use stringFromDate: just like you did.
If you need to know number values of hour/minute in a specific time zone, use -[NSCalendar components:fromDate:].
[calendar setTimeZone:...];
NSDateComponents* components = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:date];
The date formatter defaults to the local timezone. If you want a different timezone specify it. NSLog of a date used the 'NSDatedescription` method that defaults to GMT (UTC).
Examining the code:
NSDate *today = [NSDate date];
// Creates today's data in GMT (UTC) All NSDates are referenced to GMT.
NSLog(#"NSDate %#",today); (moved up for explanation ordering)
// NSDate 2015-07-01 16:07:22 +0000
// Displays the date in GMT
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
// Creates a date for matter with the system timezone
NSLog(#"Time %#", [dateFormatter stringFromDate:today]);
// Time 2015-07-01 17:07:22
// Creates a string representation in the system timezone and displays it
NSDate*stringDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:today]];
// Creates a date from the string taking into the system timezone
NSLog(#"Time date %#",stringDate);
// Time date 2015-07-01 16:07:22 +0000
// Displays the date in GMT.

NSDateFormatter return incorrect date from string

I have a method,
+ (NSDate *) convertToDateFrom:(NSString *) dateString
{
if (dateString == nil || [dateString isEqual:#""]) return nil; //return nil if dateString is empty
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMMM yyyy HH:mm"];
NSDate *date = [df dateFromString:dateString];
return date;
}
When I pass,
#"Monday, 21 November 2011 17:01" //Passed string
It returns a wrong date,
2011-11-21 23:14:00 +0000 // Output
I am not sure whether I am using those flags correctly or NSDateFormatter isn't properly converting my string to date.
Am I doing something wrong?
Thanks
The +0000 at the end of the date indicates GMT. All dates are stored relative to GMT; when you convert a date to a string or vice versa using a date formatter, the offset to your time zone is included. You can use NSDateFormatter's -setTimeZone: method to set the time zone used.
In short, you're not doing anything wrong in your code. Use [df stringFromDate:date]; to see that the date is correct. (You can also use NSDate's -descriptionWithCalendarFormat:timeZone:locale:.)
try using
df stringFromDate:date
Following worked on mine,
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [df stringFromDate:date]);
gave me output as :
Date for locale en_US: Wednesday, 26 June 2013 15:50
Try setting the time zone and locale.
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];