Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6539439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:51:34+00:00 2026-05-25T10:51:34+00:00

I need to pack string with a UTC datetime, using the smallest number of

  • 0

I need to pack string with a UTC datetime, using the smallest number of bytes/characters. I only need precision to the second. Using .NET 4.0, what would be the most space-efficient way to pack this down? Ticks doesn’t seem all that small.

All ideas appreciated.
Thanks.

EDIT: Thanks to Joel Coehoorn, the pack/unpack move is the best. Thanks! Here is some proof:

DateTimeOffset nowStamp = DateTimeOffset.UtcNow;
        Console.WriteLine( nowStamp.ToString() );                   // 9/9/2011 2:17:17 PM +00:00
        Console.WriteLine( nowStamp.ToString( "u" ) );              // 2011-09-09 14:17:17Z
        Console.WriteLine( nowStamp.Ticks.ToString() );             // 634511746376767889
        Console.WriteLine( PackDate( nowStamp ) );                  // 7R9qTgAAAAA=
        Console.WriteLine( UnpackDate( PackDate( nowStamp ) ) );    // 9/9/2011 2:17:17 PM +00:00
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:51:35+00:00Added an answer on May 25, 2026 at 10:51 am

    Perhaps a variant on unix time (seconds since 1/1/1970 rather than milliseconds) base64 encoded.

    //Helpers
    private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    public static long toUnixTime(this DateTime d)
    {
        return (long)((d.ToUniversalTime() - Jan1st1970).TotalMilliseconds);
    }
    
    public static string Base64Encode(long toEncode)
    {
        return Convert.ToBase64String(BitConverter.GetBytes(toEncode));
    }
    
    //Encode
    public static string PackDate(DateTime toPack)
    {
        return Base64Encode(toPack.toUnixTime()/1000);
    }
    
    //Decode
    public static DateTime UnpackDate(string toUnpack)
    {
        long time = BitConverter.ToInt64(Convert.FromBase64String(toUnpack),0);
        return Jan1st1970.AddSeconds(time); //you may or may not want a "ToLocaltime()" call here.
    }
    

    Note that all this was done without the aid of an IDE – there’s likely a bug or two above. But it should get you started.

    This should result in a fixed-width string. Since we’re only doing seconds rather than milliseconds, you may find you always have some extra padding in the result that you don’t need. You might even be able to get away with an int, rather than a long, which will cut the string in half. Be careful stripping that padding out, though, as the closer you get to 1970 the smaller the number, but the farther you get the larger and the more likely you are to need it. You need to be certain that your date value will fit within the new, smaller range for doing any trimming. For example, the current date fits comfortably within an int, but even 28 years from now will not. UInt32 will get you a little further into the future, but prevent you from using dates before 1970.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have need to pack four signed bytes into 32-bit integral type. this is
Hello I'm using visual studio 2008 with asp.net mvc 2. I need anti xss
I need to print escaped characters to a binary file using Ruby. The main
I'm using .NET 3.5 (C#) and the HTML Agility Pack to do some web
I need to pack all my js, but need to edit it going into
Need to an expression that returns only things with an I followed by either
need ask you about some help. I have web app running in Net 2.0.
I am trying to stop XSS attack so I am using html agility pack
I need to parse sections from a string of HTML. For example: <p>Lorem ipsum
When using the python struct module on can specify a format string that declares

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.