How to calculate the current Stardate in C#

Recently, one of our developers needed to calculate the Stardate using C#.NET.  After much internal mental gymnastics regarding TOS, TNG, and newer calculations, the following code was produced:

string stardate = DateTime.Now.Year.ToString() + “.” + ((int)(DateTime.Now.DayOfYear / 3.65F)).ToString();

The developer was immediately harangued about inconsistent time zone support.  Of course, we are not exactly sure how time zones work in outer space….

…and management wants to know which customer CR this line of code now belongs to.

1 thought on “How to calculate the current Stardate in C#

  1. Allen Gillis

    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;

Leave a Reply

Your email address will not be published. Required fields are marked *