How to covert string to date? [duplicate] - ios

This question already has answers here:
TimeZone changed while converting string to Date
(2 answers)
NSDate() or Date() shows the wrong time
(2 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Swift convert string to date output wrong date
(4 answers)
Closed 1 year ago.
my string value is "2021.09.28 23:39".
but converted date is Optional(2021-09-28 14:39:00 +0000)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy.MM.dd HH:mm"
dateFormatter.locale = Locale(identifier: "ko_kr")
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
let date = dateFormatter.date(from: "2021.09.28 23:39")
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: date!)

This is working correctly.
The KST timezone is UTC +9.
So... a time of 23:39 in the time zone KST is the same moment in time as the time 14:39 in UTC.
You output of Optional(2021-09-28 14:39:00 +0000) shows the timezone of +0000 and so is a displayed as a UTC time.

Related

Dateformatter returns different dates [duplicate]

This question already has answers here:
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 2 years ago.
Please check the code bellow:
func testDate() {
let calendar = Calendar.current
let dateEnd = calendar.date(byAdding: .day, value: 15, to: Date())
let df:DateFormatter = DateFormatter()
df.dateFormat = "MMMM dd, YYYY HH:mm"
df.timeZone = calendar.timeZone
df.timeZone = Calendar.current.timeZone
let startDateLabel = df.string(from: dateEnd!)
print(startDateLabel)
let min = df.date(from: startDateLabel)!
print(min)
}
Output
print1: March 12, 2020 14:48
print2: 2019-12-22 09:48:00 +0000
Use below dateFormat instead
df.dateFormat = "MMMM dd, yyyy HH:mm"
printing the date to console would give you the print2. It's printing the Date formats debug description. If you require a particular format you need to convert date into string with a DateFormatter. Check this link you need more help to get the desired date format.

Converting the string to date giving different format [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 3 years ago.
Converting from string to date and date to string time format is changing the original data.
Tried with dateComponents as well by giving the hour and minute
var calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour], from: calFrom)
calendar.timeZone = .current
// Specify date components
var dateComponents:DateComponents = calendar.dateComponents([.year, .month, .day, .hour], from: Date())
dateComponents.year = components.year
dateComponents.month = components.month
dateComponents.day = components.day
dateComponents.hour = 08//Cutomised hour
dateComponents.minute = 34//Cutomised Minutes
// Create date from components
let someDateTime = calendar.date(from: dateComponents)
print(someDateTime!)
Actual Output:
2019-04-02 03:04:00 +0000
Expected Output:
2019-04-02 08:34:00 +0000
I tried with below code as well. Converting the date to String and manually appending the hour and minutes to the string and converting back to the date.
let calFrom = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
var calFromDate = formatter.string(from: calFrom)
calFromDate = calFromDate + " 09" + ":30"
print(calFromDate)
//Output 02/04/2019 09:30
formatter.dateFormat = "dd/MM/yyyy hh:mm"
formatter.locale = Locale.current// set locale to reliable US_POSIX
let date1 = formatter.date(from: calFromDate)
print(date1!)
Actual Output:
2019-04-02 04:00:00 +0000
Expected Output:
02/04/2019 09:30
How to get the exact time that has given in the output?
Date used to update the hour and minute components has UTC timezone so calendar should also have the same timeZone as below,
calendar.timeZone = TimeZone(abbreviation: "UTC")!

getting nil date from string using formatter [duplicate]

This question already has an answer here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Closed 5 years ago.
I'm using this code for converting array of String date into Date
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
for i in 0 ..< data.count {
let time = data[i]
let ft_date = time["ft_date"] as! String
print(ft_date)
let dateF = formatter.date(from: ft_date)
print(dateF)
}
and it's the output of console
2017-04-09T00:00:00
Optional(2017-04-08 19:30:00 +0000)
2017-04-08T00:00:00
Optional(2017-04-07 19:30:00 +0000)
2017-04-05T00:00:00
Optional(2017-04-04 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-03-22T00:00:00
nil
why this is happening? it's the same format but I'm getting nil
Are you living in a country where daylight saving time changes on 2017-03-22 at midnight?
If yes this could be the reason because the date does not exist.
You need to change time Format ;
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

How to convert milliseconds to date string in swift 3 [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to convert milliseconds to date string in swift 3,i tried by setting date fomatter but i am not getting current date string.
var milliseconds=1477593000000
let date = NSDate(timeIntervalSince1970: TimeInterval(milliseconds))
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
formatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
print(formatter.string(from: date as Date))
output:
22-01-48793 01:30:00
Try this,
var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
print("date - \(date)")
You will get output as date :
date - 2016-10-27 18:30:00 +0000
How about trying this -
let milisecond = 1479714427
let dateVar = Date.init(timeIntervalSinceNow: TimeInterval(milisecond)/1000)
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
print(dateFormatter.string(from: dateVar))
Have a look at the documentation of NSDate:
convenience init(timeIntervalSince1970 secs: TimeInterval)
Returns an NSDate object initialized relative to the current date and time by a given number of seconds.
Just convert your milliseconds to seconds and you should get the correct date.

How to make NSDate() print the current local time [duplicate]

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Get UTC time and local time from NSDate object
(10 answers)
Closed 7 years ago.
I have tried this in playgrounds (I show in comments what it's printed):
extension NSDate {
static func currentDate() -> NSDate {
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone.localTimeZone()
calendar.locale = NSLocale.currentLocale()
let components = calendar.components([.Year, .Month, .Day], fromDate: NSDate())
components.hour = 00
components.minute = 00
components.second = 00
return calendar.dateFromComponents(components)! // "Dec 29, 2015, 12:00 AM"
}
}
print(NSDate.currentDate()) // "2015-12-28 22:00:00 +0000
Anyone has an idea what is going on here ?
I just want the current date (year/month/day). I made this extension because I had problems with NSDate(), it was off by 2 hours (showing 11.24 am, instead of 1.24 pm)
The NSDate() in Swift and [NSDate date] in Objective-C both holds the UTC date. You can not change it behaviour.
However you can create a formatter and convert to desired locale and date format and store the date as string value.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /*your desired format*/
let date = dateFormatter.dateFromString(/*your date string*/)

Resources