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

  • SEARCH
  • Home
  • 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 6133745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:16:02+00:00 2026-05-23T17:16:02+00:00

I know that I could use a format string, but I don’t want to

  • 0

I know that I could use a format string, but I don’t want to lose the culture specific representation of the date/time format. E.g.

5/4/2011 | 2:06 PM | ... should be 05/04/2011 | 02:06 PM | ...

But when I change it to a different culture, I want it to be

04.05.2011 | 14:06 | ...

without changing the format string. Is that possible?

  • 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-23T17:16:03+00:00Added an answer on May 23, 2026 at 5:16 pm

    I see only a single solution – you should obtain the current culture display format, patch it so that it meets your requirement and finally format your DateTime value using the patched format string. Here is some sample code:

    string pattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            DateTime dt = DateTime.Now;
            string[] format = pattern.Split(new string[] { CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator }, StringSplitOptions.None);
            string newPattern = string.Empty;
            for(int i = 0; i < format.Length; i++) {
                newPattern = newPattern + format[i];
                if(format[i].Length == 1)
                    newPattern += format[i];
                if(i != format.Length - 1)
                    newPattern += CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
            }
    

    As promised, here is the improved version:

    /// <summary>
    /// Extensions for the <see cref="DateTime"/> class.
    /// </summary>
    public static class DateTimeExtensions
    {
        /// <summary>
        /// Provides the same functionality as the ToShortDateString() method, but
        /// with leading zeros.
        /// <example>
        /// ToShortDateString: 5/4/2011 |
        /// ToShortDateStringZero: 05/04/2011
        /// </example>
        /// </summary>
        /// <param name="source">Source date.</param>
        /// <returns>Culture safe short date string with leading zeros.</returns>
        public static string ToShortDateStringZero(this DateTime source)
        {
            return ToShortStringZero(source,
                CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern,
                CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator);
        }
    
        /// <summary>
        /// Provides the same functionality as the ToShortTimeString() method, but
        /// with leading zeros.
        /// <example>
        /// ToShortTimeString: 2:06 PM |
        /// ToShortTimeStringZero: 02:06 PM
        /// </example>
        /// </summary>
        /// <param name="source">Source date.</param>
        /// <returns>Culture safe short time string with leading zeros.</returns>
        public static string ToShortTimeStringZero(this DateTime source)
        {
            return ToShortStringZero(source,
                CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern,
                CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator);
        }
    
        private static string ToShortStringZero(this DateTime source, 
            string pattern,
            string seperator)
        {
            var format = pattern.Split(new[] {seperator}, StringSplitOptions.None);
    
            var newPattern = string.Empty;
    
            for (var i = 0; i < format.Length; i++)
            {
                newPattern = newPattern + format[i];
                if (format[i].Length == 1)
                    newPattern += format[i];
                if (i != format.Length - 1)
                    newPattern += seperator;
            }
    
            return source.ToString(newPattern, CultureInfo.InvariantCulture);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of an algorithm that I could use to find an interesting
Does anyone know of a PDF file parser that I could use to pull
Does anyone know of an application (hosted or otherwise) that I could use to
I am new to CSLA, i just want to know what factors that could
Is it possible to know if a stream/string contains an input that could match
i know that you could organize your files according to this structure in svn:
Our dev shop currently uses Visual SourceSafe. We all know how that could end
Anyone know of a nice efficient function that could convert, for example: HelloWorld -->
Need to know this so that i could send DTMF and that is going
Do you know any stand-alone client or web tool (like grc ) that could

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.