Nsdateformatter Return Wrong Date + Swift

Swift DateFormatter returning wrong date

when hour is in am/pm(1~12) use hh, to hour in day(0~23) use HH.

This helps me format my dates:
Sample Image

Swift Dateformatter() converts wrong date

You need yyyy not YYYY

dateFormatter.dateFormat = "dd-MM-yyyy-HH:mm"

dateFromString() returning wrong date swift 3.0

The format of date should be dd/MM/yyyy not dd/mm/yyyy. The mm indicates the minutes and MM indicates the month.

And also add the below line in your code

formatter.timeZone = TimeZone(abbreviation: "GMT+0:00")

This line of code set time zone. If you not, then you get 30/11/2017 in output.

The reason behind this is when string date not contain time then formatter assume that it is midnight and you also not given the timezone so it will take current timezone.

NSDateFormatter return wrong date + Swift

When you convert from string to NSDate, if you do not set the timezone to the formatter, you will get the NSDate of a date in your local time zone. I suppose that your time zone is GMT+3 .

Then, when you show the value of 'date' (using println, NSLog but not NSDateFormatter), without setting the time zone, you will get GMT+0 time. That why you got 3h later.

Depend on how to use NSDateFormatter, you will have the date string as you want. In your case, It returns what you want, doesn't it?

Remember that NSDate presents a moment of time.

let dateString = "2016-04-02"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
println("dateString: \(dateString)")

formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formatter.dateFromString(dateString) //without specify timezone, your dateString "2016-04-02" is your local time (GMT-3),
//means it's 2016-04-02 00:00:000 at GMT+0. That is the value that NSDate holds.

println("date: \(date)") //that why it show 2016-04-01 21:00:000, but not 2016-04-02 00:00:000

formatter.dateFormat = "yyyy-MM-dd"
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)")

Swift date format returning wrong date

The code in your question is muddled up. You try to convert a string into a string in the first example and something unspecified into a Date in the second example.

Here's how to convert a Date into a String:

import Foundation

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"
let string: String = formatter.string(from: Date())

print(string) // prints for example 2020-10-18T10:54:07+01:00

Here's how to convert a string into a date

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ssZ"
let date: Date = formatter.date(from: "2020-10-18 10:59:56+0100")! // In real life, handle the optional properly
print(date) // prints 2020-10-18 09:59:56 +0000

When you print a Date directly, it automatically uses UTC as the time zone. This is why it changed it in the code above.

In the examples, I explicitly specified the type of string and date to show what type they are. Type inference means you can omit these in normal code.

As a general rule when handling dates:

  • always use Date in your code. Date is a type that stores the number of seconds since Jan 1st 1970 UTC.
  • Only convert dates to strings when displaying them to the user or communicating with an external system.
  • When calculating periods etc, always use a Calendar to get things like date components and intervals in units other than seconds. You might think to get "the same time tomorrow" you could just add 24 * 60 * 60 to a Date but in many countries, like mine, that will work on only 363 days in the year. Calendar will correctly handle things like daylight saving and leap years.

Swift datefomatter returns wrong time

There's nothing wrong. The date formatter returns the correct time

The date string

"2020-08-11T05:32:33.000Z"

represents a date in UTC(+0000).

Your local time zone is obviously UTC+0530. DateFormatter considers the local time zone by default.

To format the time also in UTC you have to set the time zone of the date formatter explicitly

func printTime(date: Date) {

let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)

print("date: \(date)")
print("Time: \(time)")
}

NSDateFormatter showing wrong date in ios?

Your local time zone is presumably UTC+5:30. You are specifying a date but not a time, so the time is implied to be midnight. Midnight on the 16th in your local time zone is 18:30 the day before (the 15th) in UTC time, which is why you get "2016-03-15 18:30:00 +0000"

When you log the date with NSLog(@"%@----",dateValue) you are actually invoking [dateValue description], which displays the date using UTC.

You can use NSLog(@"%@----",[dateValue descriptionWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone] locale:nil]) and you will see the date in your current time zone.

Be aware though that the description and associated methods such as descriptionWithCalendarFormat methods are only for debugging, you should use an NSDateFormatter to convert dates to strings. iOS_Binod's answer shows one way you could do this.



Related Topics



Leave a reply



Submit