Vicky Arora
Vicky Arora

Reputation: 501

iOS Swift Same code results in different date formats when run on iPad and iPhone

I have an iPhone app which while testing on 1x/2x on iPad results in some unusual behavior.

iPad text field output: 25-Jul-2015 6:02 pm

iPhone text field output: Jul 25, 2015, 6:02 PM

My date format code is as below:

dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "MMM/dd/yyyy HH:mm Z"    // Already tried adding dateFormat, still no help.
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .ShortStyle

Can anyone tell me what is going on here? I want both of them to use the same format. Thank you.

Upvotes: 1

Views: 336

Answers (2)

Mithun Ravindran
Mithun Ravindran

Reputation: 2312

Once you set your own date format via a string, you should not restrict it using date style or time style. You may use that if you not defining any custom date format.

You may comment on that two property.

HH -> will give you hours in 24hrs format. If you prefer to have in 12hrs format then use -> hh

moreover, z will give you time zones value. to get AM/PM you should use -> a

Change your date format to "MMM-dd-yyyy hh:mm a" And you will get a consistent output as "Jul-17-2015 12:05 PM"

Upvotes: 2

Just Variable
Just Variable

Reputation: 877

let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .ShortStyle
dateFormatter.dateFormat = "MMM/dd/yyyy HH:mm a"

the above code gives me formatted Date string

Upvotes: -1

Related Questions