473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Display a time span in days

I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't find
anything built-in that will help.

Failing that I'd like to display the date (only the date) that corresponds
to the saved time_t variable.
Thanks for any help
Jan 17 '07 #1
9 1543
" Frank" <fr***@a-znet.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't find
anything built-in that will help.
I believe dividing the number by (60*60*24) might give you the number of days
(depends on what exactly you have in that variable). Be careful about how the
result is rounded, though.
Failing that I'd like to display the date (only the date) that corresponds to
the saved time_t variable.
--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/

Jan 17 '07 #2
Thanks

Actually I thought of that after I posted. That question was an after
thought - without too much thought.

The main reason for posting is that I wanted to display a saved date,
without the time, and it seems there must be an easier way that what I was
doing.

Thanks a lot
"Sourcerer" <en****@MAKNIgmail.comwrote in message
news:eo**********@ss408.t-com.hr...
>" Frank" <fr***@a-znet.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>>I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't
find anything built-in that will help.

I believe dividing the number by (60*60*24) might give you the number of
days (depends on what exactly you have in that variable). Be careful about
how the result is rounded, though.
>Failing that I'd like to display the date (only the date) that
corresponds to the saved time_t variable.

--
"It is easy in the world to live after the world's opinion; it easy in
solitude to live after our own; but the great man is he who in the midst
of the crowd keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/

Jan 17 '07 #3

" Frank" <fr***@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Thanks

Actually I thought of that after I posted. That question was an after
thought - without too much thought.

The main reason for posting is that I wanted to display a saved date,
without the time, and it seems there must be an easier way that what I was
doing.
First gmtime or localtime to get a struct tm, then strftime with a format
string of your choice.
>
Thanks a lot
"Sourcerer" <en****@MAKNIgmail.comwrote in message
news:eo**********@ss408.t-com.hr...
>>" Frank" <fr***@a-znet.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>>>I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't
find anything built-in that will help.

I believe dividing the number by (60*60*24) might give you the number of
days (depends on what exactly you have in that variable). Be careful
about how the result is rounded, though.
>>Failing that I'd like to display the date (only the date) that
corresponds to the saved time_t variable.

--
"It is easy in the world to live after the world's opinion; it easy in
solitude to live after our own; but the great man is he who in the midst
of the crowd keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/


Jan 17 '07 #4
I'll look at that. I did tried tm once but if I remember I got 2 for example
rather than March.
Maybe strftime was what I missed.

In your prior post you cautioned about rounding. Thanks for that. I got 0
when I expected 1 until I remember your caution.
thanks

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>
" Frank" <fr***@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Thanks

Actually I thought of that after I posted. That question was an after
thought - without too much thought.

The main reason for posting is that I wanted to display a saved date,
without the time, and it seems there must be an easier way that what I
was doing.

First gmtime or localtime to get a struct tm, then strftime with a format
string of your choice.
>>
Thanks a lot
"Sourcerer" <en****@MAKNIgmail.comwrote in message
news:eo**********@ss408.t-com.hr...
>>>" Frank" <fr***@a-znet.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
I have a time saved in a time_t variable.

I'd like to be able to display the number of days since then but can't
find anything built-in that will help.

I believe dividing the number by (60*60*24) might give you the number of
days (depends on what exactly you have in that variable). Be careful
about how the result is rounded, though.

Failing that I'd like to display the date (only the date) that
corresponds to the saved time_t variable.

--
"It is easy in the world to live after the world's opinion; it easy in
solitude to live after our own; but the great man is he who in the midst
of the crowd keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/



Jan 17 '07 #5

" Frank" <fr***@a-znet.comwrote in message
news:eH**************@TK2MSFTNGP02.phx.gbl...
I'll look at that. I did tried tm once but if I remember I got 2 for
example rather than March.
Maybe strftime was what I missed.

In your prior post you cautioned about rounding. Thanks for that. I got 0
when I expected 1 until I remember your caution.
Wasn't my post actually, but...

Define what you mean by "number of days since then". If the saved time is
11:59 PM on Feb 1, and current system time is 12:03 AM on Feb 2, the delay
is only 4 minutes, but midnight passed... so do you count that as a day?
Rounding won't really help you there.

If you mean calendar days, then you may want to convert your time_t to a tm
using localtime, zero the hour/minute/second fields, use mkgmtime to go back
to time_t, then difftime and divide by (24*60*60). In that way you will
count days elapsed since the beginning of the day in which the saved time
occurred.
>

thanks

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>
" Frank" <fr***@a-znet.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Thanks

Actually I thought of that after I posted. That question was an after
thought - without too much thought.

The main reason for posting is that I wanted to display a saved date,
without the time, and it seems there must be an easier way that what I
was doing.

First gmtime or localtime to get a struct tm, then strftime with a format
string of your choice.
>>>
Thanks a lot
"Sourcerer" <en****@MAKNIgmail.comwrote in message
news:eo**********@ss408.t-com.hr...
" Frank" <fr***@a-znet.comwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl.. .
>I have a time saved in a time_t variable.
>
I'd like to be able to display the number of days since then but can't
find anything built-in that will help.

I believe dividing the number by (60*60*24) might give you the number
of days (depends on what exactly you have in that variable). Be careful
about how the result is rounded, though.

Failing that I'd like to display the date (only the date) that
corresponds to the saved time_t variable.

--
"It is easy in the world to live after the world's opinion; it easy in
solitude to live after our own; but the great man is he who in the
midst of the crowd keeps with perfect sweetness the independence of
solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/




Jan 17 '07 #6
Ben Voigt wrote:
If you mean calendar days, then you may want to convert your time_t
to a tm using localtime, zero the hour/minute/second fields, use
mkgmtime to go back to time_t, then difftime and divide by
(24*60*60). In that way you will count days elapsed since the
beginning of the day in which the saved time occurred.
or just do the division first, then the subtraction:

int num_days = (end_time_t / 86400) - (start_time_t/86400);

Will always give the difference in day number between two time_t values. If
you want the "rollover" from one day to the next to be at a time other than
midnight (e.g. to counts days in a different timezone than the one
represented in your time_t), then just subtract the appropriate number of
hours*3600 from each time_t before dividing.

-cd
Jan 17 '07 #7
Thanks for all the insight.

I'm showing the days since and also the prior date.

I got two extra characters displayed so I added the last line shown below.

Can you tell me what is going on?

Thanks again

struct tm pasttime;

localtime_s( &pasttime, &OldTime );

strftime( szTmpStr, 100, "%A %B %d, %Y\n", &pasttime );

szTmpStr[lstrlen(szTmpStr)-1] = 0;
Jan 17 '07 #8

" Frank" <fr***@a-znet.comwrote in message
news:Ox**************@TK2MSFTNGP03.phx.gbl...
Thanks for all the insight.

I'm showing the days since and also the prior date.

I got two extra characters displayed so I added the last line shown below.
That extra line removes the newline character you have in the format string.
Perhaps it's also going through newline translation and becoming a CR/LF
pair.

Try just removing the "\n" from the format string.
>
Can you tell me what is going on?

Thanks again

struct tm pasttime;

localtime_s( &pasttime, &OldTime );

strftime( szTmpStr, 100, "%A %B %d, %Y\n", &pasttime );

szTmpStr[lstrlen(szTmpStr)-1] = 0;


Jan 17 '07 #9
ouch!

thanks

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:es**************@TK2MSFTNGP02.phx.gbl...
>
" Frank" <fr***@a-znet.comwrote in message
news:Ox**************@TK2MSFTNGP03.phx.gbl...
>Thanks for all the insight.

I'm showing the days since and also the prior date.

I got two extra characters displayed so I added the last line shown
below.

That extra line removes the newline character you have in the format
string. Perhaps it's also going through newline translation and becoming a
CR/LF pair.

Try just removing the "\n" from the format string.
>>
Can you tell me what is going on?

Thanks again

struct tm pasttime;

localtime_s( &pasttime, &OldTime );

strftime( szTmpStr, 100, "%A %B %d, %Y\n", &pasttime );

szTmpStr[lstrlen(szTmpStr)-1] = 0;



Jan 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: The Biscuit Eater | last post by:
Greetings from a second day newbie to php. I think I have figured out a way to explode a field in a csv file (like 11-08-03) and implode it as 031108 and then compare it to the current date(ymd)...
2
by: Tyrone Slothrop | last post by:
I am coding a site which involves uploading images. All of the PHP and display is OK but the client wants to be able to display the image thumbnail on the upload page and show the full image on...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
5
by: Trint Smith | last post by:
I need to display a timer ticking down. Example: 11h:52m:39s to 00h:00m:00s How can I do this? Thanks, Trint .Net programmer trintsmith@hotmail.com
18
by: Max | last post by:
This is a follow-up on my previous thread concerning having the program wait for a certain date and time and then executing some code when it gets there. My question is; can I use the Sleep...
3
by: DotNetGuy | last post by:
I have been programming in asp.net w/ vb for 3 years now and I feel a need to include more client side script with javascript so here I am. Hopefully in the right place. I have a timecard web app...
5
by: Haines Brown | last post by:
A much discussed issue, but I'm unable to get results. Using galeon as browser. I have a div, the bottem border of which is solid and serves as a base line. My aim is to line up next to each...
1
by: rbinington | last post by:
Hi, I am trying to write a DNN module that has the ability to insert articles into an article repository. I want the users to be able to move pages around and enter text into the FCKEditor. I...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.