By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

calculate number of days since Jan 1, 0000

P: n/a
I cant seem to get that date into any DateTime to make my calculation
directly by subtracting "01-01-0000" from "now".

After reading this:
http://www.mathworks.com/matlabcentr...bjectType=File

I kluged up this routine that works:
// convert date time into that funny matlab serial date time that starts at
jan 1, 0000
private string DT2Matlab(DateTime thisDT)
{
DateTime y1970 = Convert.ToDateTime("JAN-1-1970");
thisDT = thisDT.AddDays(719529);
// supposidly 719529 days from "0 CE" to 1970
TimeSpan ts = thisDT.Subtract(y1970);
return ts.TotalDays.ToString("#.####");
}

Seems there should be a cleaner way to calculate "Days.fraction_of_days"
from "0 CE" all in one swoop? "01-01-0001" gives the wrong answer when
subtracting and the 0000 is a no go.

...thanks for looking..
--
================================================== ====================
Joseph "Beemer Biker" Stateson
http://TipsForTheComputingImpaired.com
http://ResearchRiders.org Ask about my 99'R1100RT
================================================== ====================
Jun 26 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
There is no year zero, is there? I believe it goes 3 BC, 2 BC, 1 BC, 1 AD, 2
AD, ...

"Beemer Biker" wrote:
I cant seem to get that date into any DateTime to make my calculation
directly by subtracting "01-01-0000" from "now".

After reading this:
http://www.mathworks.com/matlabcentr...bjectType=File

I kluged up this routine that works:
// convert date time into that funny matlab serial date time that starts at
jan 1, 0000
private string DT2Matlab(DateTime thisDT)
{
DateTime y1970 = Convert.ToDateTime("JAN-1-1970");
thisDT = thisDT.AddDays(719529);
// supposidly 719529 days from "0 CE" to 1970
TimeSpan ts = thisDT.Subtract(y1970);
return ts.TotalDays.ToString("#.####");
}

Seems there should be a cleaner way to calculate "Days.fraction_of_days"
from "0 CE" all in one swoop? "01-01-0001" gives the wrong answer when
subtracting and the 0000 is a no go.

...thanks for looking..
--
================================================== ====================
Joseph "Beemer Biker" Stateson
http://TipsForTheComputingImpaired.com
http://ResearchRiders.org Ask about my 99'R1100RT
================================================== ====================
Jun 26 '07 #2

P: n/a
"ModelBuilder" <Mo**********@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
There is no year zero, is there? I believe it goes 3 BC, 2 BC, 1 BC, 1
AD, 2
AD, ...
Correct.

Also, just as an aside, "old" dates can be a bit tricky if the solution is
trying to store them in SQL Server:
http://weblogs.sqlteam.com/mladenp/a.../16/52754.aspx
--
http://www.markrae.net

Jun 26 '07 #3

P: n/a
PS
"Beemer Biker" <js*******@swri.eduwrote in message
news:13*************@corp.supernews.com...
>I cant seem to get that date into any DateTime to make my calculation
directly by subtracting "01-01-0000" from "now".

After reading this:
http://www.mathworks.com/matlabcentr...bjectType=File

I kluged up this routine that works:
// convert date time into that funny matlab serial date time that starts
at jan 1, 0000
private string DT2Matlab(DateTime thisDT)
{
DateTime y1970 = Convert.ToDateTime("JAN-1-1970");
thisDT = thisDT.AddDays(719529);
// supposidly 719529 days from "0 CE" to 1970
TimeSpan ts = thisDT.Subtract(y1970);
return ts.TotalDays.ToString("#.####");
}

Seems there should be a cleaner way to calculate "Days.fraction_of_days"
from "0 CE" all in one swoop? "01-01-0001" gives the wrong answer when
subtracting and the 0000 is a no go.
Just factor in the extra days that you need when you use DateTime.MinValue
in your method. Why don't you give us a specific date and the number that
you want to see as representing that date?

PS
>
..thanks for looking..
--
================================================== ====================
Joseph "Beemer Biker" Stateson
http://TipsForTheComputingImpaired.com
http://ResearchRiders.org Ask about my 99'R1100RT
================================================== ====================


Jun 26 '07 #4

P: n/a
Beemer Biker wrote:
I cant seem to get that date into any DateTime to make my calculation
directly by subtracting "01-01-0000" from "now".

After reading this:
http://www.mathworks.com/matlabcentr...bjectType=File
I kluged up this routine that works:
// convert date time into that funny matlab serial date time that starts
at jan 1, 0000
private string DT2Matlab(DateTime thisDT)
{
DateTime y1970 = Convert.ToDateTime("JAN-1-1970");
thisDT = thisDT.AddDays(719529);
// supposidly 719529 days from "0 CE" to 1970
TimeSpan ts = thisDT.Subtract(y1970);
return ts.TotalDays.ToString("#.####");
}

Seems there should be a cleaner way to calculate "Days.fraction_of_days"
from "0 CE" all in one swoop? "01-01-0001" gives the wrong answer when
subtracting and the 0000 is a no go.
As has been said, there is no year 0, just as there is no month 0.

You might also want to be very careful about calendars - we've skipped
days in the past when calendars used were changed.

Alun Harford
Jun 26 '07 #5

P: n/a
"Alun Harford" <de*****@alunharford.co.ukwrote in message
news:eA**************@TK2MSFTNGP04.phx.gbl...
Beemer Biker wrote:
>I cant seem to get that date into any DateTime to make my calculation
directly by subtracting "01-01-0000" from "now".

After reading this:
http://www.mathworks.com/matlabcentr...bjectType=File I
kluged up this routine that works:
// convert date time into that funny matlab serial date time that starts
at jan 1, 0000
private string DT2Matlab(DateTime thisDT)
{
DateTime y1970 = Convert.ToDateTime("JAN-1-1970");
thisDT = thisDT.AddDays(719529);
// supposidly 719529 days from "0 CE" to 1970
TimeSpan ts = thisDT.Subtract(y1970);
return ts.TotalDays.ToString("#.####");
}

Seems there should be a cleaner way to calculate "Days.fraction_of_days"
from "0 CE" all in one swoop? "01-01-0001" gives the wrong answer when
subtracting and the 0000 is a no go.

As has been said, there is no year 0, just as there is no month 0.
Thanks Alun! I read where the year "0" is just a reference point, as
explained here: http://tinyurl.com/39b9es

quoteing "datenum is one of three conversion functions that enable you to
express dates and times in any of three formats in MATLAB: a string (or date
string), a vector of date and time components (or date vector), or as a
numeric offset from a known date in time (or serial date number). Here is an
example of a date and time expressed in the three MATLAB formats:

Date String: '24-Oct-2003 12:45:07'
Date Vector: [2003 10 24 12 45 07]
Serial Date Number: 7.3188e+005

A serial date number represents the whole and fractional number of days from
a specific date and time, where datenum('Jan-1-0000 00:00:00') returns the
number 1. (The year 0000 is merely a reference point and is not intended to
be interpreted as a real year in time.)"

I was looking for a clean (??) C# function to calculate that 7.3188e+005
Googleing I found a UTC -Matlab that used 1970. I thought there might be
a clean way to get the time span in days.fraction_of_days out of the
DateTime function. What I have works, though it is probably not accurate
by "days in the past" let alone leap-seconds. I have a C# program that
generates a text file that is being used as input to a matlab program. The
engineer wants time in that funny number that matlab uses and was not happy
when I put out a date time string accurate down into milliseconds.
You might also want to be very careful about calendars - we've skipped
days in the past when calendars used were changed.

Alun Harford
Jun 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.