Code:
let isoDate = "1981-02-20T10:44:00+0800"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "" }
let date = dateFormatter.date(from: isoDate)!
dateFormatter.string(from: date)
Output:
Why does the date output timezone is +07:30 instead of +0800 as defined in the date string and also TimeZone.current? Anybody can explain this?
The timezone format is not Z but ZZZ
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZ"
Related
I am getting date string from server like that "2017-08-05T00:30:00.000+02:00". Below is my code for date formatter.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
But it returns 2017-08-04 22:30:00 +0000. Which is wrong. Any suggestions?
In Swift 3
You can change your server time string to UTC time Date as:
let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Your Code for parsing date is correct: 2017-08-05T00:30:00.000+02:00 and 2017-08-04 22:30:00 +0000 represent the same time, just in different time zones. Your only problem is that Date doesn't actually store the time zone
yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ
This is correct date formate for your input string. Here is the Apple Document for more description.
Here is my code:
var strInputDateString: String = "2017-08-05T00:30:00.000+02:00"
var dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ"
//Set new dateFormate
var date1: Date? = dateFormat.date(from: strInputDateString)
dateFormat.dateFormat = "dd-MM-YYYY hh:mm:ss"
// Your Desire
var strOutputDateString: String = dateFormat.string(from: date1!)
print("\(strInputDateString)")
print("\(strOutputDateString)")
output:
Main input String:2017-08-05T00:30:00.000+02:00
Converted String: 05-08-2017 04:00:00
For now, I tried to convert your date format with my custom. It's give me the perfect output.
Actually, your code is good.
I copied your data and ran it in playground.
What kind of format you want it be?
Try this
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Hope this helps
The step you are missing is to again use your DateFormatter to format the date into a string. When you use print on the date, it will always show GMT. For illustration purposes, I've set the TimeZone to Berlin to match the original offset of +2:00.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
// Hard set to Berlin
dateFormatter.timeZone = TimeZone(identifier: "Europe/Berlin")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
// Your missing step
dateFormatter.string(from: currentDate)
Remember that DateFormatters are about:
Parsing from a String - df.date(from: String)
Formatting from a Date - df.string(from: Date)
My date is "2017-05-04 13:46:36.0". How can I filter only the date from this?
I have used this function:
func toDate(dateString : String, dateFormat : String = "yyyy-MM-dd'T'HH:mm:ssX")-> NSDate!{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let convertedDate = dateFormatter.dateFromString(dateString)
return convertedDate
}
let date = "2017-05-04 13:46:36.0"
now I have tried tried to set:
lbl.text = String.toDate(dateString: date, dateFormat: "yyyy-MM-dd")
But it always returns nil and crashes the app? Why is this happening?
You have wrong Parameter passed
var dateString: String = "2017-05-04 13:46:36.0"
var dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var yourDate: Date? = dateFormatter1.date(from: dateString)
dateFormatter1.dateFormat = "yyyy-MM-dd"
Rule of Date formatter is you must set date format same like your string while you are getting date from string , if mismatch then you will get null
Swift 2
var dateString: String = "2017-05-04 13:46:36.0"
var dateFormatter1: NSDateFormatter = NSDateFormatter()
dateFormatter1.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var yourDate: NSDate = dateFormatter1.dateFromString(dateString)
dateFormatter1.dateFormat = "yyyy-MM-dd"
print("\(dateFormatter1.stringFromDate(yourDate))")
let dateString = "2017-05-04 13:46:36.0"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.0"
let date = dateFormatter.date(from: dateString)!
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.string(from: date)
HI I am trying to formate date from string (08-05-1988) I want to convert to date as given below, output also given below,
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
let stringValue = String(describing: dateFromString)
out put : Optional(1988-05-07 18:30:00 +0000)
Then, I want to format the out put date from (1988-05-07 18:30:00 +0000) to other format as given below. But, when format the string to Date is nil
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
let formatter = DateFormatter()
formatter.dateFormat = "dd-MMM-yyyy"
let formatedDate:String = formatter.string(from: date!)
Expected Output : 08-May-1988
The output Optional(1988-05-07 18:30:00 +0000) is the result of the description of a Date object. It's not related to any given string format.
To convert 08-05-1988 to 08-May-1988 you can simply use
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd-MMM-yyyy"
let formatedDate = dateFormatter.string(from: dateFromString!)
let date = Date()
let format = DateFormatter()
format.dateFormat = "dd-MM-yyyy"
let formatDate = format.string(from: date)
You're calling dateFormatter.date on dateString instead of dateFromString. Since "08-05-1988" doesn't match the specified format of "yyyy-mm-dd hh:mm:ss Z", dateFormatter.date returns nil.
I want to convert 2016-03-09 00:00:00 to 09/03/2016
I am try below code but print nil value
var date: [String] = ["2016-03-09 00:00:00", "2016-03-20 00:00:00"]
for i in date {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/YYYY"
let showDate = dateFormatter.dateFromString(i)
print(showDate)
}
above code print nil
pls help me how to print date dd/MM/YYYY format in swift
I think your date format is wrong. change "dd/MM/YYYY" with "YYYY-MM-dd HH:mm:s" and try ...
You have a date like "2016-03-09 00:00:00" .. so format should be "YYYY-MM-dd HH:mm:s"
for i in date {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:s"
if let showDate = dateFormatter.dateFromString(i){
print("Date with Time: \(showDate)")
dateFormatter.dateFormat = "YYYY-MM-dd"
let resultString = dateFormatter.stringFromDate(showDate)
print("final result: \(resultString)")
}
}
Result:-
Date with Time: 2016-03-09 00:00:00 +0000
final result: 2016-03-09
Date with Time: 2016-03-20 00:00:00 +0000
final result: 2016-03-20
You should first create a dateformatter to handle the type. Then sort your format such as:
let date: [String] = ["2016-03-09 00:00:00", "2016-03-20 00:00:00"]
for i in date {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:s"
let showDate:NSDate? = dateFormatter.dateFromString(i)
dateFormatter.dateFormat = "dd/MM/YYYY"
let date = dateFormatter.stringFromDate(showDate!)
print(date)
}
Rather than reconfiguring a single date formatter on each pass through the loop in #ElCaptain's answer I would suggest creating 2 outside the loop: an input date formatter for converting from "YYYY-MM-dd HH:mm:s" date strings to NSDates, and an output formatter for converting from NSDates to "YYYY-MM-dd" format.
let inputFormatter = NSDateFormatter()
inputFormatter.dateFormat = "YYYY-MM-dd HH:mm:s"
let outputFormatter = NSDateFormatter()
ourputFormatter.dateFormat = "YYYY-MM-dd"
for i in date {
if let showDate = inputFormatter.dateFromString(i) {
print("Date with Time: \(showDate)")
let resultString = outputFormatter.stringFromDate(showDate)
print("final result: \(resultString)")
}
}
I'm trying to convert a string to NSDate here is my code
let strDate = "2015-11-01T00:00:00Z" // "2015-10-06T15:42:34Z"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ssZ"
print ( dateFormatter.dateFromString( strDate ) )
I keep getting nil as a result
The "T" in the format string needs to be single quoted so it will not be consider a symbol:
Swift 3.0
let strDate = "2015-11-01T00:00:00Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from:strDate)
print("date: \(date!)")
Output:
date: 2015-11-01 00:00:00 +0000
Swift 2.x
let strDate = "2015-11-01T00:00:00Z"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.dateFromString(strDate)
print("date: \(date!)")
Output:
date: 2015-11-01 00:00:00 +0000
See: Date Field SymbolTable.
This includes the need to enclose ASCII letters in single quotes if they are intended to represent literal text.
For swift 4 and swift 3.2 updated answer.
here all Date related function mentioned.
hop these function useful for you.
1=> Timestamp to Date
func timeStampToDate(_timestamp : String, _dateFormat : String) -> String{
var LOCAL_TIME_ZONE: Int { return TimeZone.current.secondsFromGMT() }
var date = Date(timeIntervalSince1970: TimeInterval(_timestamp)!)
date += TimeInterval(LOCAL_TIME_ZONE as NSNumber)
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "GMT") //Set timezone that you want
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = _dateFormat //Specify your format that you want
let strDate = dateFormatter.string(from: date)
return strDate
}
2=> Date to String
func DateToString(date : Date, dateFormatte : String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormatte
print("Dateobj: (dateFormatter.string(from: dateObj!))")
return (dateFormatter.string(from: date as Date))
}
3=> String to date
func StringDateToDate(dateString : String, dateFormatte : String) -> Date {
//let dateString = "Thu, 22 Oct 2015 07:45:17 +0000"
//let dateFormatte = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormatte
let dateObj = dateFormatter.date(from: dateString)
if dateObj == nil {
return Date()
}
return dateObj!
}