How to Convert String to Date Without Time in Swift 3

How to convert string to date without time in Swift 3?

let dateWithTime = Date()

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short

let date = dateFormatter.string(from: dateWithTime) // 2/10/17

This will format it correctly, but this will be for display purposes only. All Dates have time.

How to convert string to date without change time swift

You can add a timezone to dateFormatter

dateFromat.timeZone = TimeZone(secondsFromGMT: 0)

Updated code:

let scheduleDate : String = "2020-01-25 20:11:00"
let dateFromat : DateFormatter = DateFormatter()
dateFromat.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFromat.timeZone = TimeZone.init(secondsFromGMT: 0)
let dateFromString = dateFromat.date(from: scheduleDate)
print(dateFromString as Any)

ios Swift Date without Time

Let me guess, you are in the GMT+6 time zone, aren't you?

When Dates are printed, they always show up in UTC.

2019-04-14 18:00 UTC is the same as 2019-04-15 00:00 in your local time zone.

Your code is not wrong. It works fine.

To see it in your time zone, use a DateFormatter and set the timeZone property:

let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateStyle = .full
formatter.timeZone = TimeZone.current
print(formatter.string(from: Date().onlyDate))

Convert string to date in Swift

  • Convert the ISO8601 string to date

      let isoDate = "2016-04-14T10:44:00+0000"

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date = dateFormatter.date(from:isoDate)!
  • Get the date components for year, month, day and hour from the date

      let calendar = Calendar.current
    let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)
  • Finally create a new Date object and strip minutes and seconds

      let finalDate = calendar.date(from:components)

Consider also the convenience formatter ISO8601DateFormatter introduced in iOS 10 / macOS 10.12:

let isoDate = "2016-04-14T10:44:00+0000"

let dateFormatter = ISO8601DateFormatter()
let date = dateFormatter.date(from:isoDate)!

How to convert Swift Date or String to timestamp without time zone of PostgreSQL?

It's no 100% solution I wanted but it's also very good.

import Vapor
import FluentPostgreSQL

final class TestModel: PostgreSQLModel {

static var createdAtKey: TimestampKey? = \.createdAt

var id: Int?
var someValue: Int
var someOtherProprty: String
var createdAt: Date?

init(someValue: Int, someOtherProprty: String) {
self.someValue = someValue
self.someOtherProprty = someOtherProprty
}

}

extension TestModel: Content {
}

extension TestModel: Migration {
}

extension TestModel: Parameter {
}

How to convert String to Date getting only date and not time in Swift?

Several issues. First, your date format is all wrong. You have the wrong timezone. You have the wrong locale. Once these are fixed you can parse the date string.

Then the proper way to determine if a date is today or tomorrow (ignoring time), is to use the Calendar methods isDateInToday and isDateInTomorrow.

Here's your code with everything fixed:

func dateFormatter(date: String){
let dateFormatter = DateFormatter()
// For a string like "2018-06-09T09:20:48"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) // assume date is GMT+0

if let date_db = dateFormatter.date(from: date) {
if Calendar.current.isDateInToday(date_db) {
print("It is today")
} else if Calendar.current.isDateInTomorrow(date_db) {
print("It is tomorrow")
} else {
print("After Some Days")
}
} else {
print("Unexpected date string")
}
}

How can I convert string date to NSDate?

try this:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /* find out and place date format from
* http://userguide.icu-project.org/formatparse/datetime
*/
let date = dateFormatter.dateFromString(/* your_date_string */)

For further query, check NSDateFormatter and DateFormatter classes of Foundation framework for Objective-C and Swift, respectively.

Swift 3 and later

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = /* date_format_you_want_in_string from
* http://userguide.icu-project.org/formatparse/datetime
*/
guard let date = dateFormatter.date(from: /* your_date_string */) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}

// use date constant here

Edit:

Alternative date time format reference
https://unicode-org.github.io/icu/userguide/format_parse/datetime/



Related Topics



Leave a reply



Submit