How to convert NSString into NSDate [duplicate] - ios

This question already has answers here:
NSDate is 5 hours off
(4 answers)
Closed 7 years ago.
I want to convert my label.text into NSDate My formatter is MM/dd/yyyy. So I convert it like this.
mydate1=[dateFormatter dateFromString:depdate1txt.text];
My label value is (lldb) po depdate1txt.text
12/13/2015
But when I check the mydate1 it shows as (lldb) po mydate1
2015-12-12 18:30:00 +0000
Why date has changed into 12th december in 2015?
Please help me.
Thanks

When you print the date with po it's not formatting it - that's the base value. All dates are stored as a timestamp and only formatted when you explicitly format them.
The po command just takes a default formatter to show the date.
Oh, and the 'date change' is because of your timezone - the printed date is GMT +0.

Related

Date format always return nil when using 12hr date format [duplicate]

This question already has answers here:
iOS NSDate Comparison works differently when the 24-Hour Time in date settings toggles between ON and OFF?
(2 answers)
DateFormatter doesn't return date for "HH:mm:ss"
(1 answer)
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 4 years ago.
I'm trying to convert a string to a date, using the following code:
let dateFormmater = DateFormatter();
dateFormmater.timeZone = TimeZone(abbreviation: "UTC");
dateFormmater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z";
let date = dateFormmater.date(from: "2018-06-13T15:33:47.796Z");
When the iPhone is set to 24hr mode, this works perfectly, and a new date object is created, but when the iPhone is set to 12hr mode, the date formatter returns nil.
Also, trying to get a string from date using the same date format while the iPhone is set to 24hr mode, results in a normal date, but using this in 12hr mode, results in the hours being presented both in 12hr and 24hr format,
e.g: 2018-07-05T208:17:22.019 PMZ.
note that the hour is 208 PM...
Has anyone encountered this before, and has a solution for this?
Cheers,
P.s this problem replicate able 1:1 in objective-c, using the corresponding classes.

Ruby/Rails date conversion using strptime. MM/DD/YY [duplicate]

This question already has answers here:
Rails formatting date
(4 answers)
Closed 5 years ago.
I'm parsing PDF which returns date as string in format "04/27/17". How can I convert these strings to format for storing as date in rails?
Thanks!
Date::strptime can initialize a date object from a wacky US-based Y2K-unaware date format. Then if you have an active record class called MyObject with a date field called sweet_date, it would go like this:
date = Date::strptime("04/27/17", "%m/%d/%y") # returns Thu, 27 Apr 2017
object = MyObject.first
object.sweet_date = date
object.save # true

Use [NSDate date] to get current time, but the return value is wrong [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 6 years ago.
I am using a MacBook developing my iOS app & run it on a real iPhone. Both my MacBook & my iPhone show me current time is 2016-08-11 13:18
But at the same time, the following code shows me the current time that is 3 hours earlier than now:
NSLog(#"NOW = %#",[NSDate date]); // it prints out 2016-08-11 10:18:17 +0000
But NSDate documentation tells me the +date function returns current date and time. Why I get a date time three hours earlier then?
When you log an NSDate using NSLog it is always shown in UTC. It sounds like you are in the UTC+3 time zone, so a time shown in UTC will be 3 hours earlier.
If you want to display a date in your local time zone create a date formatter and use it to display the date instead. (By default date for matters use the user's local time zone.)
An NSDate does not have an inherent time zone. An NSDate records an instant in time. You have to use a time zone in order to look at it.
Your date is correct. You are just displaying it in a way that's confusing.

NSDateFormatter for this format

I want to make an NSDateFormatter object for this date format (for example):
2016-04-15 15:00:00
I've tried yyyy-mm-dd hh:mm:ss but then when I am trying to attach it to a date like that:
date = [formatter dateFromString:dateString];
It gives me nil.
what is the correct way to do it?
Thank you!
set date format as
2016-04-15 15:00:00
yyyy-MM-dd HH:mm:ss
your date string is 24 hour format
Here's all you need for date formatting:
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
There's a list of link under the Fixed Format title where you can see in detail the different formats for whatever you want, for iOS7 and greater see this:
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

iOS Converting milliseconds to date returns wrong time

I am trying to convert milliseconds into date. Below shown is the code i am using.
double startDateDb=1380275880000;
NSDate *date=[NSDate dateWithTimeIntervalSince1970:(startDateDb/1000.0)];
NSLog(#"date---%#",date);
My log gives date as 2013-09-27 09:58:00 +0000
When i use online tool to convert i am getting "9/27/13 5:58 AM" which is correct.
Please help me to fix the issue.
NSLog used your timezone when displaying dates. TO get the date in UTC use the NSDate methods and specify the tie zone. All NSDates are UTC timezone based.
Use NSDateFormatter to display the date/time in another timezone.
Your NSLog put the timezone in GMT, the timezone you're looking for is GMT-4.

Resources