

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you create a date object from a date in Swift xcode in iOS?
To create a date object in swift we’ll use DateComponents() of swift. We can do this In two ways. We’ll use Playground to test our code instead of simulator.
We’ll use date component and calendar to create a date. We can create date component in two ways.
Method 1
Creating date using the default initializer of DateComponent().
var date = DateComponents.init( calendar: <#T##Calendar?#>, timeZone: <#T##TimeZone?#>, era: <#T##Int?#>, year: <#T##Int?#>, month: <#T##Int?#>, day: <#T##Int?#>, hour: <#T##Int?#>, minute: <#T##Int?#>, second: <#T##Int?#>, nanosecond: <#T##Int?#>, weekday: <#T##Int?#>, weekdayOrdinal: <#T##Int?#>, quarter: <#T##Int?#>, weekOfMonth: <#T##Int?#>, weekOfYear: <#T##Int?#>, yearForWeekOfYear: <#T##Int?#>)
This will ask all the things like calendar type, date, day, month, year and other things required to create a date.
Method 2
We can create the same using only init method of DateComponent()
var date = DateComponents() date.day = 5 date.month = 6 date.year = 1993
These are the two ways to create date component, now we need to convert date components to date. We can do it by using the below code.
let cal = Calendar.current let newDate = cal.date(from: date) print("the date is", newDate!)
When we run the above code result would be
- Related Questions & Answers
- How do you create a date object from a date in Swift xcode?
- How to create JavaScript Date object from date string?
- Working with Xcode Auto Layout in Swift and iOS
- How to create a Date object in JavaScript?
- How do you convert a JavaScript date to UTC?
- How do I create a date picker in tkinter?
- How to create Date object from String value in Java?
- How to create date object in Java?
- How to create a WebView in an iOS App using Swift?
- How to convert a Python date string to a date object?
- How to convert a JS Date to a Python date object?
- How do I subtract minutes from a date in JavaScript?
- How do you create a list from a set in Java?
- Create a Date object using the Calendar class in Java
- How do you get a directory listing sorted by creation date in Python?
Advertisements