I have got the following time stamp in milliseconds:
NSDate * date = [NSDate dateWithTimeIntervalSince1970:1470524933.923123];
NSDate * date2 = [NSDate dateWithTimeIntervalSince1970:1470666561.000];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm:ss"];
NSString * dateInString = [dateFormatter stringFromDate:date];
When I run it I get the following:
2016-08-07 at 00:08:53, 2016-08-08 at 15:29:21
However the first date should be as following according to the epoch time converter website I am using:
Your time zone: 8/7/2016, 12:08:53 AM GMT+1:00 DST
It says 00 rather than 12. Why is that?
Try to set the TimeZone of your NSDateFormatter to GMT
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
As larme and vadian suggested if you want 12 for hour use this format
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' hh:mm:ss a"];
Related
I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000
The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.
try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
I'm getting date string from server and its GMT+2 i think and unable to convert into indian GMT 5:30 format Here is the server date string 2017-01-11T05:08:15.157Z and i want this in indian time format i have tried below code but getting null
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.Z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormatter dateFromString:stmp]; // create date from string
[dateFormatter setDateFormat:#"dd-MM-yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
NSLog(#"New Date%#", timestamp);
How to convert this into 11-01-2017 08:14 PM format.
Thanks in advance.
Its Done by making small change i.e instead of giving date format yyyy-MM-dd'T'HH:mm:ss.Z changede to yyyy-MM-dd'T'HH:mm:ss.zzzZ its working fine here is the code i have changed We can change any GMT to system local time zone Thank you
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.zzzZ"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormatter dateFromString:stmp]; // create date from string
[dateFormatter setDateFormat:#"dd-MM-yyyy h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
NSLog(#"New Date%#", timestamp);
here is the output New Date23-12-2016 4:47 PM
In my iOS application(Objective C) I am adding date (29th Nov 2016) in database in following format:
2016-11-29 04:08:00 PM
And during displaying it I want to show in following format:
11/29/2016 04:08:00 PM
For this I have added formator like
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date1 = [dateFormatter dateFromString: StartDateTime];
//here StartDateTime = 2016-11-29 04:08:00 PM
// Convert to new Date Format with /
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *newDate = [dateFormatter stringFromDate:date1];
But here newDate returns nil.
How can I change the date format? Please suggest me.
Thank you.
I have tried your code with below changes:
NSString *StartDateTime = #"2016-11-29 04:08:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date1 = [dateFormatter dateFromString: StartDateTime];
//here StartDateTime = 2016-11-29 04:08:00 PM
// Convert to new Date Format with /
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *newDate = [dateFormatter1 stringFromDate:date1];
NSLog(#"%#",newDate);
Here with in your code I have added a new object of NSDateFormatter (For above example dateFormatter1) with new allocation and new date formate.
You will get your answer with this way.
Make sure every time that you have to create new instance of NSDateFormatter every time you want to change the DateTime formate.
And even after trying this option, If you don't get your desired result then please try to run the code in Device, not in Simulator. Because simulator may not return accurate result all the time especially for date and time
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.
I have locale date and time I am using the following code
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(#"current time:%#",currentTime);
Here i got localTime
Now i am converting local time to GMT Time
I am using the following code
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"hh:mm:ss MM dd yyyy "];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter1 setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
Now my problem is i want to convert this GMT date into 10digit format like 1362468453
For that purpose i am using
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
How to get GMT Time in tendigits format
Use timeIntervalSince1970 function for your date to get epoch time.
NSLog(#"Epoch Time : %f", [dateFromString timeIntervalSince1970]);
Check this NSDate Class Reference