Click here to Skip to main content
13,557,707 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 5 Aug 2010
Licenced CPOL

Stardates in C#

, 5 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Calculate a *Proper* Star Trek style Stardate
Just a little bit of boredom here, and I happened to be watching an episode of Star Trek: TNG, so I decided I would cobble up a little function to calculate a valid stardate.

Fairly novel function, but who knows...someone writing a video game could use it.

public double calculateStardate()
     DateTime calenderStarTrek = new DateTime(2323, 1, 1, 0, 0, 0);
     // you can replace the DateTime.Now with year values running all
     // the way back to January 1, 1946 at 0:00:00 and still maintain 
     // a positive stardate by virtue of the way the calculation is 
     // done, but this is contingent upon application of a 377 year 
     // offset which puts us into the current Trek year.  Follow the 
     // code for a little bit clearer understanding...
     DateTime presentLocalDate = DateTime.Now;
     // derive the total offset between present date and trek date
     // if we don't do the year offset, we will end up with a date
     // that is in the negative, which while technically correct
     // it's probably not what we want so we adjust the year value
     // of the current date to bring us into the proper Trek year
     presentLocalDate = presentLocalDate.AddYears(377);
     TimeSpan timeOffset = presentLocalDate - calenderStarTrek;
     // we divide into a highly granular value to get the most
     // accurate value possible for our calculation.  What we are
     // actually figuring out the average number of seconds in a
     // 4 year leap/non-leap cycle and dividing the total number of
     // milliseconds in our time offset to arrive at our raw stardate
     // value.
     // we further round this value to 2 decimal places and miliply it
     // by 100 in rounded form, and then divide by 100 to get our two 
     // decimal places back. 2.7 stardate units account for 1 earth day
     // so we do the rounding and multiply / divide operations to get 
     // granularity back into the final date value.
     // it makes sense when you look at it :-)  trust me.
     double yearValue = timeOffset.TotalMilliseconds / (60 * 60 * 24 * 365.2422);
     double stardate = Math.Floor(yearValue * 100);
     stardate = stardate / 100;
     return stardate;

Comments, suggestions and code rewrites appreciated. I handle criticism well :-) Cheers.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Bruno Tagliapietra28-Sep-13 2:55
memberBruno Tagliapietra28-Sep-13 2:55 
QuestionUtcNow vs local Now Pin
hype89126-Jul-13 16:12
memberhype89126-Jul-13 16:12 
GeneralMy vote of 5 Pin
Ajdin Idrizi4-Jan-13 12:28
memberAjdin Idrizi4-Jan-13 12:28 
GeneralReason for my vote of 5 Out of the Box thinking.. Pin
GPUToaster8-Aug-10 19:39
memberGPUToaster8-Aug-10 19:39 
GeneralReason for my vote of 5 Live Long and Prosper. Have a five f... Pin
linuxjr5-Aug-10 5:33
memberlinuxjr5-Aug-10 5:33 
GeneralReason for my vote of 5 Just because... :-) Pin
digital man2-Aug-10 23:04
memberdigital man2-Aug-10 23:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 5 Aug 2010
Article Copyright 2010 by JohnCAldrich
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid