2

Hi I want to get time as 11:40 from 2017-07-31T11:40:00.000Z

Below is the code I am using:

let formatter = Foundation.DateFormatter()
formatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.sssZ" //2017-04-01T18:05:00.000
let date1  = formatter.date(from: data.arvTime)
print("date:\(String(describing: date1))")
let dateFormatterPrint = Foundation.DateFormatter()
dateFormatterPrint.dateFormat = "HH:mm"
let resultTime = dateFormatterPrint.string(from: date1!)
print("time:\(String(describing: resultTime))")

Getting time as 1:29 i.e. I am getting wrong time.

Please help me to solve this issue.

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
user2931321
  • 468
  • 1
  • 7
  • 28
  • Add the timezone information of your input string as well in the date formatter.. it should give you right time – Kapil G Jul 31 '17 at 07:27

4 Answers4

14

use the dateformat

formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

instead of

formatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.sssZ" 

for full code

 let formatter = Foundation.DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" //2017-04-01T18:05:00.000
    let date1  = formatter.date(from: "2017-04-01T18:05:00.000Z")
    print("date:\(String(describing: date1))") 
    formatter.dateFormat = "HH:mm"
    let resultTime = formatter.string(from: date1!)
    print("time:\(String(describing: resultTime))")

option-2

    let formatter = Foundation.DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ" //2017-04-01T18:05:00.000
    let date1  = formatter.date(from: "2017-04-01T18:05:00.000Z")
    print("date:\(String(describing: date1))")
    formatter.timeZone = TimeZone(abbreviation: "UTC")
    formatter.dateFormat = "HH:mm"
    let resultTime = formatter.string(from: date1!)
    print("time:\(String(describing: resultTime))")

output

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • @Karthik Thank you very much – user2931321 Jul 31 '17 at 07:34
  • let string = "2017-01-27T18:36:36Z" let formatter = Foundation.DateFormatter() formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" let date1 = formatter.date(from: string) print("date:\(date1!)") print(dateFormatter.string(from: date1!)) formatter.timeZone = TimeZone(abbreviation: "UTC") formatter.dateFormat = "HH:mm a" let resultTime = formatter.string(from: date1!) print("time:\(String(describing: resultTime))") – Yalamandarao Jun 21 '23 at 07:06
  • date:2017-01-27 18:36:36 +0000 January 28, 2017 time:18:36 PM Why data is different .. how to get correct date ? – Yalamandarao Jun 21 '23 at 07:08
1

NSLog always prints the date object in GMT timezone, not your local timezone. use your local time zone

suma
  • 9
  • 1
0

Try this Use the timeZone property of the dateFormatter.

let formatter = Foundation.DateFormatter()
formatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.sssZ"
let date1  = formatter.date(from: "2017-07-31T11:40:00.000Z")
print("date:\(String(describing: date1))")
let dateFormatterPrint = Foundation.DateFormatter()
dateFormatterPrint.timeZone = TimeZone(abbreviation: "UTC")
dateFormatterPrint.dateFormat = "HH:mm"
let resultTime = dateFormatterPrint.string(from: date1!)
print("time:\(String(describing: resultTime))")
Aravind A R
  • 2,674
  • 1
  • 15
  • 25
0

For kotlin create Extension you can check it out

The first Step convert the String to local Date here dateFormat is Your input data formate and timeZone is input Time Zoon

fun String.toDate(dateFormat: String = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'", timeZone: TimeZone = TimeZone.getTimeZone("UST")): Date? {
val parser = SimpleDateFormat(dateFormat, Locale.getDefault())
parser.timeZone = timeZone
return parser.parse(this)}

Secon Step is to convert local time into time formate you want like : 2022 23 12 10:50:05

fun Date.formatTo(dateFormat: String, timeZone: TimeZone = TimeZone.getDefault()): String {
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
formatter.timeZone = timeZone
return formatter.format(this)}

Simple call these functions like this

fun String.convertToLocalTime():String{
val localFormat="dd-MM-yyyy HH:mm:ss"

this.toDate()?.formatTo(dateFormat = localFormat)?.let {
    return it
}

return ""}