- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- How do you create a date object from a date in Swift xcode?
- How to create JavaScript Date object from date string?
- How to create a Date object in JavaScript?
- Working with Xcode Auto Layout in Swift and iOS
- 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 do you convert a JavaScript date to UTC?
- How do I subtract minutes from a date in JavaScript?
- How to get current date and time from internet in iOS?
- Create a Date object using the Calendar class in Java
- How to convert a Python date string to a date object?
- How to add 1 day to a Date in iOS/iPhone?
- How to create a WebView in an iOS App using Swift?
- How do I display the current date and time in an iOS application?

Advertisements