Find difference in two dates in iOS2019 Community Moderator ElectionGetting the difference between two NSDates in (months/days/hours/minutes/seconds)What's the difference between the atomic and nonatomic attributes?NSDate beginning of day and end of dayGet the difference between dates like Facebook comments on iOSSwift Beta performance: sorting arraysNSDate: Extract Date ONLYHow do I make an attributed string using Swift?Getting the difference between two NSDates in (months/days/hours/minutes/seconds)Swift date formatting, getting the date back from server is not the same as sent to the serverFinding the difference between two dates always prints 10Get difference between two dates
Why is there an extra space when I type "ls" in the Desktop directory?
Vocabulary for giving just numbers, not a full answer
Virginia employer terminated employee and wants signing bonus returned
Rationale to prefer local variables over instance variables?
Do I really need to have a scientific explanation for my premise?
Gaining more land
School performs periodic password audits. Is my password compromised?
What is the population of Romulus in the TNG era?
Why restrict private health insurance?
The meaning of ‘otherwise’
What is the generally accepted pronunciation of “topoi”?
Can I negotiate a patent idea for a raise, under French law?
Making a kiddush for a girl that has hard time finding shidduch
When Schnorr signatures are part of Bitcoin will it be possible validate each block with only one signature validation?
Is this Paypal Github SDK reference really a dangerous site?
Why do we say ‘pairwise disjoint’, rather than ‘disjoint’?
Does Christianity allow for believing on someone else's behalf?
Can we track matter through time by looking at different depths in space?
Power Strip for Europe
Are there historical instances of the capital of a colonising country being temporarily or permanently shifted to one of its colonies?
I can't die. Who am I?
What is better: yes / no radio, or simple checkbox?
Possible to detect presence of nuclear bomb?
Signed and unsigned numbers
Find difference in two dates in iOS
2019 Community Moderator ElectionGetting the difference between two NSDates in (months/days/hours/minutes/seconds)What's the difference between the atomic and nonatomic attributes?NSDate beginning of day and end of dayGet the difference between dates like Facebook comments on iOSSwift Beta performance: sorting arraysNSDate: Extract Date ONLYHow do I make an attributed string using Swift?Getting the difference between two NSDates in (months/days/hours/minutes/seconds)Swift date formatting, getting the date back from server is not the same as sent to the serverFinding the difference between two dates always prints 10Get difference between two dates
I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds.
public func captureStartTime()
captureStartDateTime = Date()
public func captureEndTime(eventType: String, eventElement: String)
let difference = Date().timeIntervalSince(captureStartDateTime)
let interval = Int(difference)
let seconds = interval % 60
let secondsDescrp = String(format: "%02d", seconds)
ios swift nsdateformatter nsdatecomponents nsdatecomponentsformatter
add a comment |
I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds.
public func captureStartTime()
captureStartDateTime = Date()
public func captureEndTime(eventType: String, eventElement: String)
let difference = Date().timeIntervalSince(captureStartDateTime)
let interval = Int(difference)
let seconds = interval % 60
let secondsDescrp = String(format: "%02d", seconds)
ios swift nsdateformatter nsdatecomponents nsdatecomponentsformatter
2
Possible duplicate of Getting the difference between two NSDates in (months/days/hours/minutes/seconds)
– Rocky
Mar 6 at 4:43
1
Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds?
– Chris Shaw
Mar 6 at 4:43
As your code standsdifference
is the number of seconds between now andcaptureStartDateTime
– MadProgrammer
Mar 6 at 4:43
add a comment |
I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds.
public func captureStartTime()
captureStartDateTime = Date()
public func captureEndTime(eventType: String, eventElement: String)
let difference = Date().timeIntervalSince(captureStartDateTime)
let interval = Int(difference)
let seconds = interval % 60
let secondsDescrp = String(format: "%02d", seconds)
ios swift nsdateformatter nsdatecomponents nsdatecomponentsformatter
I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds.
public func captureStartTime()
captureStartDateTime = Date()
public func captureEndTime(eventType: String, eventElement: String)
let difference = Date().timeIntervalSince(captureStartDateTime)
let interval = Int(difference)
let seconds = interval % 60
let secondsDescrp = String(format: "%02d", seconds)
ios swift nsdateformatter nsdatecomponents nsdatecomponentsformatter
ios swift nsdateformatter nsdatecomponents nsdatecomponentsformatter
edited Mar 6 at 4:45
iOS Team
1,1401319
1,1401319
asked Mar 6 at 4:39
KashifKashif
53621235
53621235
2
Possible duplicate of Getting the difference between two NSDates in (months/days/hours/minutes/seconds)
– Rocky
Mar 6 at 4:43
1
Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds?
– Chris Shaw
Mar 6 at 4:43
As your code standsdifference
is the number of seconds between now andcaptureStartDateTime
– MadProgrammer
Mar 6 at 4:43
add a comment |
2
Possible duplicate of Getting the difference between two NSDates in (months/days/hours/minutes/seconds)
– Rocky
Mar 6 at 4:43
1
Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds?
– Chris Shaw
Mar 6 at 4:43
As your code standsdifference
is the number of seconds between now andcaptureStartDateTime
– MadProgrammer
Mar 6 at 4:43
2
2
Possible duplicate of Getting the difference between two NSDates in (months/days/hours/minutes/seconds)
– Rocky
Mar 6 at 4:43
Possible duplicate of Getting the difference between two NSDates in (months/days/hours/minutes/seconds)
– Rocky
Mar 6 at 4:43
1
1
Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds?
– Chris Shaw
Mar 6 at 4:43
Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds?
– Chris Shaw
Mar 6 at 4:43
As your code stands
difference
is the number of seconds between now and captureStartDateTime
– MadProgrammer
Mar 6 at 4:43
As your code stands
difference
is the number of seconds between now and captureStartDateTime
– MadProgrammer
Mar 6 at 4:43
add a comment |
4 Answers
4
active
oldest
votes
interval
is the answer you want. That is the total number of seconds between the two dates.
Your seconds
value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
add a comment |
you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc
let dateString1 = "2019-03-07T14:20:20.000Z"
let dateString2 = "2019-03-07T14:20:40.000Z"
//set date formate
let Dateformatter = DateFormatter()
Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//convert string to date
let dateold = Dateformatter.date(from: dateString1)!
let datenew = Dateformatter.date(from: dateString2)!
//use default datecomponents with two dates
let calendar1 = Calendar.current
let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateold, to: datenew)
let seconds = components.second
print("Seconds: (seconds)")
New contributor
add a comment |
Use the following code to get the difference between two dates, Store current time in startTime
when pressed button 1 and store current date time in endTime
when pressed button 2, See this code, I hope this helps you.
var startTime:Date!
var endTime:Date!
@IBAction func buttonStartTime(_ sender: UIButton)
startTime = Date()
@IBAction func buttonEndTime(_ sender: UIButton)
endTime = Date()
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.second]
formatter.unitsStyle = .full
let difference = formatter.string(from: startTime, to: endTime)!
print(difference)//output "8 seconds"
Output
8 seconds
add a comment |
let startDate = NSDate()
let endDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(NSCalendarUnit.CalendarUnitSecond, fromDate: startDate, toDate: endDate, options: nil)
let seconds = dateComponents.second
println("Seconds: (seconds)")
New contributor
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%2f55015683%2ffind-difference-in-two-dates-in-ios%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
interval
is the answer you want. That is the total number of seconds between the two dates.
Your seconds
value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
add a comment |
interval
is the answer you want. That is the total number of seconds between the two dates.
Your seconds
value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
add a comment |
interval
is the answer you want. That is the total number of seconds between the two dates.
Your seconds
value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.
interval
is the answer you want. That is the total number of seconds between the two dates.
Your seconds
value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.
edited Mar 6 at 4:49
answered Mar 6 at 4:43
rmaddyrmaddy
244k27323384
244k27323384
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
add a comment |
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
Thanks for sharing, however still interval is not giving exact second values. There is difference if 10 - 15 seconds between previous and current date. I have also used NSCalendar still its not giving accurate one.
– Kashif
Mar 6 at 4:55
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
You need to update your question with more specific details. Show the exact values being used and your actual and expected results.
– rmaddy
Mar 6 at 4:56
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
I need to get seconds between two button clicks, so on one button click, i have retrieved date as captureStartDateTime, on second button click i retrieved current date and time and try to find seconds in interval.
– Kashif
Mar 6 at 5:02
add a comment |
you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc
let dateString1 = "2019-03-07T14:20:20.000Z"
let dateString2 = "2019-03-07T14:20:40.000Z"
//set date formate
let Dateformatter = DateFormatter()
Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//convert string to date
let dateold = Dateformatter.date(from: dateString1)!
let datenew = Dateformatter.date(from: dateString2)!
//use default datecomponents with two dates
let calendar1 = Calendar.current
let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateold, to: datenew)
let seconds = components.second
print("Seconds: (seconds)")
New contributor
add a comment |
you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc
let dateString1 = "2019-03-07T14:20:20.000Z"
let dateString2 = "2019-03-07T14:20:40.000Z"
//set date formate
let Dateformatter = DateFormatter()
Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//convert string to date
let dateold = Dateformatter.date(from: dateString1)!
let datenew = Dateformatter.date(from: dateString2)!
//use default datecomponents with two dates
let calendar1 = Calendar.current
let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateold, to: datenew)
let seconds = components.second
print("Seconds: (seconds)")
New contributor
add a comment |
you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc
let dateString1 = "2019-03-07T14:20:20.000Z"
let dateString2 = "2019-03-07T14:20:40.000Z"
//set date formate
let Dateformatter = DateFormatter()
Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//convert string to date
let dateold = Dateformatter.date(from: dateString1)!
let datenew = Dateformatter.date(from: dateString2)!
//use default datecomponents with two dates
let calendar1 = Calendar.current
let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateold, to: datenew)
let seconds = components.second
print("Seconds: (seconds)")
New contributor
you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc
let dateString1 = "2019-03-07T14:20:20.000Z"
let dateString2 = "2019-03-07T14:20:40.000Z"
//set date formate
let Dateformatter = DateFormatter()
Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//convert string to date
let dateold = Dateformatter.date(from: dateString1)!
let datenew = Dateformatter.date(from: dateString2)!
//use default datecomponents with two dates
let calendar1 = Calendar.current
let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateold, to: datenew)
let seconds = components.second
print("Seconds: (seconds)")
New contributor
New contributor
answered Mar 7 at 4:57
Kaushik KapadiyaKaushik Kapadiya
111
111
New contributor
New contributor
add a comment |
add a comment |
Use the following code to get the difference between two dates, Store current time in startTime
when pressed button 1 and store current date time in endTime
when pressed button 2, See this code, I hope this helps you.
var startTime:Date!
var endTime:Date!
@IBAction func buttonStartTime(_ sender: UIButton)
startTime = Date()
@IBAction func buttonEndTime(_ sender: UIButton)
endTime = Date()
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.second]
formatter.unitsStyle = .full
let difference = formatter.string(from: startTime, to: endTime)!
print(difference)//output "8 seconds"
Output
8 seconds
add a comment |
Use the following code to get the difference between two dates, Store current time in startTime
when pressed button 1 and store current date time in endTime
when pressed button 2, See this code, I hope this helps you.
var startTime:Date!
var endTime:Date!
@IBAction func buttonStartTime(_ sender: UIButton)
startTime = Date()
@IBAction func buttonEndTime(_ sender: UIButton)
endTime = Date()
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.second]
formatter.unitsStyle = .full
let difference = formatter.string(from: startTime, to: endTime)!
print(difference)//output "8 seconds"
Output
8 seconds
add a comment |
Use the following code to get the difference between two dates, Store current time in startTime
when pressed button 1 and store current date time in endTime
when pressed button 2, See this code, I hope this helps you.
var startTime:Date!
var endTime:Date!
@IBAction func buttonStartTime(_ sender: UIButton)
startTime = Date()
@IBAction func buttonEndTime(_ sender: UIButton)
endTime = Date()
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.second]
formatter.unitsStyle = .full
let difference = formatter.string(from: startTime, to: endTime)!
print(difference)//output "8 seconds"
Output
8 seconds
Use the following code to get the difference between two dates, Store current time in startTime
when pressed button 1 and store current date time in endTime
when pressed button 2, See this code, I hope this helps you.
var startTime:Date!
var endTime:Date!
@IBAction func buttonStartTime(_ sender: UIButton)
startTime = Date()
@IBAction func buttonEndTime(_ sender: UIButton)
endTime = Date()
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.second]
formatter.unitsStyle = .full
let difference = formatter.string(from: startTime, to: endTime)!
print(difference)//output "8 seconds"
Output
8 seconds
answered Mar 6 at 6:33
AtulParmarAtulParmar
1,108521
1,108521
add a comment |
add a comment |
let startDate = NSDate()
let endDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(NSCalendarUnit.CalendarUnitSecond, fromDate: startDate, toDate: endDate, options: nil)
let seconds = dateComponents.second
println("Seconds: (seconds)")
New contributor
add a comment |
let startDate = NSDate()
let endDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(NSCalendarUnit.CalendarUnitSecond, fromDate: startDate, toDate: endDate, options: nil)
let seconds = dateComponents.second
println("Seconds: (seconds)")
New contributor
add a comment |
let startDate = NSDate()
let endDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(NSCalendarUnit.CalendarUnitSecond, fromDate: startDate, toDate: endDate, options: nil)
let seconds = dateComponents.second
println("Seconds: (seconds)")
New contributor
let startDate = NSDate()
let endDate = NSDate()
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components(NSCalendarUnit.CalendarUnitSecond, fromDate: startDate, toDate: endDate, options: nil)
let seconds = dateComponents.second
println("Seconds: (seconds)")
New contributor
New contributor
answered Mar 6 at 5:52
yaswardhanyaswardhan
12
12
New contributor
New contributor
add a comment |
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%2f55015683%2ffind-difference-in-two-dates-in-ios%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
2
Possible duplicate of Getting the difference between two NSDates in (months/days/hours/minutes/seconds)
– Rocky
Mar 6 at 4:43
1
Date.timeIntervalSince(_:) will give you the interval in seconds. By the % 60 operation, are you trying to convert this to minutes and seconds?
– Chris Shaw
Mar 6 at 4:43
As your code stands
difference
is the number of seconds between now andcaptureStartDateTime
– MadProgrammer
Mar 6 at 4:43