What is the correct way to get date of 18 years ago from today's Date in Swift3?
In Swift2, I had
let startingDate = NSDate(timeIntervalSinceNow: -600000000)
If you want date with 18 year less you can use Calendar unit year for that.
let date = Calendar.current.date(byAdding: .year, value: -18, to: Date())
Output
Related
I have datecomponent objects that represent some time in the future. I want to calculate how many dates from now until that date. I'm also including representation of the dates simply as dates. What I'm finding is that when I am trying to show how many there are to a date that is 'tomorrow' it's showing 0. To my mind it should be showing 1. I can try a hacky way of just adding 1 to my count but I'm wondering is it because it's trying to round to the nearest 24 hours or something? If so how can I 'fix' it?
Here is my sample code:
let myPreviousRelevantDate = self.datePickerOutlet.date
let nextDate = Date(timeInterval: Double(86400 * (myDurationInDaysAsInt)), since: myPreviousRelevantDate!)
let daysToNextDate = Calendar.current.dateComponents([.day], from: Date(), to: nextDate).day!
What I'd like to avoid is the number of days to the target date changing during the day also - i.e. regardless of the timestamp of my target date - the number of days to that day remaining constant until midnight is reached.
If your intent is to calculate the number of days using a timeless calendrical calculation what you need is to use noon time. Note that not every day has 24 hours, you should always use calendar method to add days to a date:
extension Date {
var noon: Date {
Calendar(identifier: .iso8601)
.date(
bySettingHour: 12,
minute: 0,
second: 0,
of: self
)!
}
}
let daysToNextDate = Calendar.current.dateComponents([.day], from: Date().noon, to: nextDate.noon).day!
I'm attempting to convert a Date to a DateComponents, change a few properties, and get a new Date back. I've written the following code:
import Foundation
var components = Calendar.current.dateComponents(in: .current, from: Date())
components.day = 7
components.month = 3
components.year = 1900
DateFormatter.localizedString(from: components.date!, dateStyle: .long, timeStyle: .long)
I've tested this in the America/Denver time zone/locale on an iOS Simulator on iOS 15.0 as well as a Swift REPL on my Mac running the latest macOS Big Sur and Xcode 13.0, and in both places, I get approximately the following output at the time of this writing:
March 7, 2021 at 9:38:13 AM MST
Everything about this is as expected, except for the year. I had explicitly set the year to be 1900, but the year in the output is 2021. How can I make DateComponents honor the year when generating a date, or how can I do this same kind of thing manually so it'll actually work?
If you get all components from a date with dateComponents(in:from:), you have to set also yearForWeekOfYear accordingly
components.yearForWeekOfYear = 1900
Or specify only the date and time components including calendar and timeZone you really need for example
var components = Calendar.current.dateComponents([.calendar, .timeZone, .year, .month, .day, .hour, .minute, .second], from: Date())
components.day = 7
components.month = 3
components.year = 1900
DateFormatter.localizedString(from: components.date!, dateStyle: .long, timeStyle: .long)
Note that you are getting all the date components, including weekOfYear, weekOfMonth, dayOfWeek, and most importantly, yearForWeekOfYear. When you set the date components to 1900-03-07, you did not set all those components correctly either, and when resolving a Date from a DateComponents that has such conflicting components, the algorithm just so happens to prefer the year they got from yearForWeekOfYear.
If you set those fields to nil, you will get the expected result:
var components = Calendar.current.dateComponents(in: .current, from: Date())
components.day = 7
components.month = 3
components.year = 1900
components.yearForWeekOfYear = nil
There will still be conflicts in the date components though, and although this doesn't cause a crash, I'm not sure if this behaviour will change in the future. I suggest that you get just the components you need. For example:
var components = Calendar.current.dateComponents([
.calendar, .timeZone, .era, .year, .month, .day, .hour, .minute, .second, .nanosecond
], from: Date())
I have a UIDatePicker and I want to show only the dates of specific weekday in of the picker.
For example, if I select Mon 26 March then I should only see
2 April,
9 April,
16 April,
23 April
and so on in the UIDatePicker. How can this be achieved using UIDatePicker
I Think there is no build-in functionality that can be used to accomplish this , you can create a pickerView and get week dates by adding this to the current
extension Date
{
var nextDateWeak : Date {
return Calendar.current.date(byAdding: .day, value: 6 , to: self )!
}
}
This question already has answers here:
NSDate of yesterday
(6 answers)
How to add minutes to current time in swift
(11 answers)
Closed 5 years ago.
I use this code to get the current date
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy.MM.dd"
let result = formatter.string(from: date)
Any idea on how can I get the date in the past, for example a day or a year before the current date?
Try to use this:
Date(timeIntervalSinceNow: -3600); \\One hour
Or you can use Calendar for this
let calendar = Calendar.current
var component = DateComponents()
component.year = -1
calendar.date(byAdding: component, to: Date())
I am trying to convert today's date using DateComponents:
let calendar = Calendar.current
//add today's date
var todayDate = Date()
var dateComponents = calendar.dateComponents([.day, .month, .year], from: todayDate)
dateComponents.timeZone = TimeZone.current
todayDate = calendar.date(from: dateComponents)!
While debugging I found that after declaring todayDate, its value was 2016-11-11 07:44:44 +0000. After using dateComponents, the value changed to 2016-11-10 18:30:00 +0000. Whereas according to my location, the day should be 11th November, and the time should be somewhere between 1 or 2 PM. Why is this happening?
You haven't specified a timezone, nor hours minutes and seconds in your components. So, all these values are assumed to be zero. The resulting time is at midnight GMT on the year, month and day that you specified.
When you printed the result, you specified your current timezone, which appears to be 5h 30m different.
I'm guessing you are in India.