How to calculate days, hours, minutes from now to certain date in swift
Calculate the number of days, hours, minutes and seconds to another date in swift.
I have already tried using swifts datecomponents to and from function but it is returning the incorrect amount of days for me.
@objc func createCountdown(){
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.day, .hour, .minute, .second], from: date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
var birthdayDate = DateComponents()
//Get user's birthday
birthdayDate.month = 4
birthdayDate.day = 16
birthdayDate.hour = 0
birthdayDate.minute = 0
let birthday = userCalendar.date(from: birthdayDate as DateComponents)
let BirthdayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: birthday!)
let daysLeft = BirthdayDifference.day
let hoursLeft = BirthdayDifference.hour
let minutesLeft = BirthdayDifference.minute
let secondsLeft = BirthdayDifference.second
daysLabel.text = String(daysLeft!)
hoursLabel.text = String(hoursLeft!)
minutesLabel.text = String(minutesLeft!)
secondsLabel.text = String(secondsLeft!)
}
The test date in the code is the birthday values. I'm simply trying to make a day, hour, minute, and second countdown to the specified birthday from the current date.
The output for this is 74 days, but obviously it is more than 74 days from today, 12/31/2018 to april 16th, 4/16/2019. That is around 105 days, not 74. Any advice is greatly appreciated.
Thank you.
swift
add a comment |
Calculate the number of days, hours, minutes and seconds to another date in swift.
I have already tried using swifts datecomponents to and from function but it is returning the incorrect amount of days for me.
@objc func createCountdown(){
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.day, .hour, .minute, .second], from: date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
var birthdayDate = DateComponents()
//Get user's birthday
birthdayDate.month = 4
birthdayDate.day = 16
birthdayDate.hour = 0
birthdayDate.minute = 0
let birthday = userCalendar.date(from: birthdayDate as DateComponents)
let BirthdayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: birthday!)
let daysLeft = BirthdayDifference.day
let hoursLeft = BirthdayDifference.hour
let minutesLeft = BirthdayDifference.minute
let secondsLeft = BirthdayDifference.second
daysLabel.text = String(daysLeft!)
hoursLabel.text = String(hoursLeft!)
minutesLabel.text = String(minutesLeft!)
secondsLabel.text = String(secondsLeft!)
}
The test date in the code is the birthday values. I'm simply trying to make a day, hour, minute, and second countdown to the specified birthday from the current date.
The output for this is 74 days, but obviously it is more than 74 days from today, 12/31/2018 to april 16th, 4/16/2019. That is around 105 days, not 74. Any advice is greatly appreciated.
Thank you.
swift
add a comment |
Calculate the number of days, hours, minutes and seconds to another date in swift.
I have already tried using swifts datecomponents to and from function but it is returning the incorrect amount of days for me.
@objc func createCountdown(){
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.day, .hour, .minute, .second], from: date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
var birthdayDate = DateComponents()
//Get user's birthday
birthdayDate.month = 4
birthdayDate.day = 16
birthdayDate.hour = 0
birthdayDate.minute = 0
let birthday = userCalendar.date(from: birthdayDate as DateComponents)
let BirthdayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: birthday!)
let daysLeft = BirthdayDifference.day
let hoursLeft = BirthdayDifference.hour
let minutesLeft = BirthdayDifference.minute
let secondsLeft = BirthdayDifference.second
daysLabel.text = String(daysLeft!)
hoursLabel.text = String(hoursLeft!)
minutesLabel.text = String(minutesLeft!)
secondsLabel.text = String(secondsLeft!)
}
The test date in the code is the birthday values. I'm simply trying to make a day, hour, minute, and second countdown to the specified birthday from the current date.
The output for this is 74 days, but obviously it is more than 74 days from today, 12/31/2018 to april 16th, 4/16/2019. That is around 105 days, not 74. Any advice is greatly appreciated.
Thank you.
swift
Calculate the number of days, hours, minutes and seconds to another date in swift.
I have already tried using swifts datecomponents to and from function but it is returning the incorrect amount of days for me.
@objc func createCountdown(){
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.day, .hour, .minute, .second], from: date)
let currentDate = calendar.date(from: components)
let userCalendar = Calendar.current
var birthdayDate = DateComponents()
//Get user's birthday
birthdayDate.month = 4
birthdayDate.day = 16
birthdayDate.hour = 0
birthdayDate.minute = 0
let birthday = userCalendar.date(from: birthdayDate as DateComponents)
let BirthdayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: birthday!)
let daysLeft = BirthdayDifference.day
let hoursLeft = BirthdayDifference.hour
let minutesLeft = BirthdayDifference.minute
let secondsLeft = BirthdayDifference.second
daysLabel.text = String(daysLeft!)
hoursLabel.text = String(hoursLeft!)
minutesLabel.text = String(minutesLeft!)
secondsLabel.text = String(secondsLeft!)
}
The test date in the code is the birthday values. I'm simply trying to make a day, hour, minute, and second countdown to the specified birthday from the current date.
The output for this is 74 days, but obviously it is more than 74 days from today, 12/31/2018 to april 16th, 4/16/2019. That is around 105 days, not 74. Any advice is greatly appreciated.
Thank you.
swift
swift
asked Dec 31 '18 at 23:31
SpykeSpyke
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to find out when is the next birth date based on the day and month of birthday. You can use Calendar's method nextDate(after: Date, matching: DateComponents)
func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = default, direction: Calendar.SearchDirection = default) -> Date?
let birthDateCoponents = DateComponents(month: 4, day: 16)
let nextBirthDate = Calendar.current.nextDate(after: Date(), matching: birthDateCoponents, matchingPolicy: .nextTime)!
let difference = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: nextBirthDate)
difference.day // 105
difference.hour // 2
difference.minute // 5
difference.second // 30
When displaying it to the user you can use DateComponentsFormatter with the appropriate unitsStyle
. You can see below how it would look like when using .full
style and limiting the units to .day, .hour, .minute, .second
:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .full
formatter.string(from: Date(), to: nextBirthDate) // "105 days, 1 hour, 44 minutes, 36 seconds"
1
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992129%2fhow-to-calculate-days-hours-minutes-from-now-to-certain-date-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to find out when is the next birth date based on the day and month of birthday. You can use Calendar's method nextDate(after: Date, matching: DateComponents)
func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = default, direction: Calendar.SearchDirection = default) -> Date?
let birthDateCoponents = DateComponents(month: 4, day: 16)
let nextBirthDate = Calendar.current.nextDate(after: Date(), matching: birthDateCoponents, matchingPolicy: .nextTime)!
let difference = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: nextBirthDate)
difference.day // 105
difference.hour // 2
difference.minute // 5
difference.second // 30
When displaying it to the user you can use DateComponentsFormatter with the appropriate unitsStyle
. You can see below how it would look like when using .full
style and limiting the units to .day, .hour, .minute, .second
:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .full
formatter.string(from: Date(), to: nextBirthDate) // "105 days, 1 hour, 44 minutes, 36 seconds"
1
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
add a comment |
You need to find out when is the next birth date based on the day and month of birthday. You can use Calendar's method nextDate(after: Date, matching: DateComponents)
func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = default, direction: Calendar.SearchDirection = default) -> Date?
let birthDateCoponents = DateComponents(month: 4, day: 16)
let nextBirthDate = Calendar.current.nextDate(after: Date(), matching: birthDateCoponents, matchingPolicy: .nextTime)!
let difference = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: nextBirthDate)
difference.day // 105
difference.hour // 2
difference.minute // 5
difference.second // 30
When displaying it to the user you can use DateComponentsFormatter with the appropriate unitsStyle
. You can see below how it would look like when using .full
style and limiting the units to .day, .hour, .minute, .second
:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .full
formatter.string(from: Date(), to: nextBirthDate) // "105 days, 1 hour, 44 minutes, 36 seconds"
1
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
add a comment |
You need to find out when is the next birth date based on the day and month of birthday. You can use Calendar's method nextDate(after: Date, matching: DateComponents)
func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = default, direction: Calendar.SearchDirection = default) -> Date?
let birthDateCoponents = DateComponents(month: 4, day: 16)
let nextBirthDate = Calendar.current.nextDate(after: Date(), matching: birthDateCoponents, matchingPolicy: .nextTime)!
let difference = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: nextBirthDate)
difference.day // 105
difference.hour // 2
difference.minute // 5
difference.second // 30
When displaying it to the user you can use DateComponentsFormatter with the appropriate unitsStyle
. You can see below how it would look like when using .full
style and limiting the units to .day, .hour, .minute, .second
:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .full
formatter.string(from: Date(), to: nextBirthDate) // "105 days, 1 hour, 44 minutes, 36 seconds"
You need to find out when is the next birth date based on the day and month of birthday. You can use Calendar's method nextDate(after: Date, matching: DateComponents)
func nextDate(after date: Date, matching components: DateComponents, matchingPolicy: Calendar.MatchingPolicy, repeatedTimePolicy: Calendar.RepeatedTimePolicy = default, direction: Calendar.SearchDirection = default) -> Date?
let birthDateCoponents = DateComponents(month: 4, day: 16)
let nextBirthDate = Calendar.current.nextDate(after: Date(), matching: birthDateCoponents, matchingPolicy: .nextTime)!
let difference = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: nextBirthDate)
difference.day // 105
difference.hour // 2
difference.minute // 5
difference.second // 30
When displaying it to the user you can use DateComponentsFormatter with the appropriate unitsStyle
. You can see below how it would look like when using .full
style and limiting the units to .day, .hour, .minute, .second
:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .full
formatter.string(from: Date(), to: nextBirthDate) // "105 days, 1 hour, 44 minutes, 36 seconds"
edited Jan 1 at 0:16
answered Dec 31 '18 at 23:54
Leo DabusLeo Dabus
133k32274347
133k32274347
1
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
add a comment |
1
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
1
1
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
Wow. I spent 10 hours on this problem. Your generous help and advice is greatly appreciated. Thank you so much Leo, I really appreciate you. This has solved my problem.
– Spyke
Jan 1 at 1:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992129%2fhow-to-calculate-days-hours-minutes-from-now-to-certain-date-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown