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 7681985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:27:58+00:00 2026-05-31T18:27:58+00:00

I have many strings like 20120117 and 20120321. I need to convert it in

  • 0

I have many strings like “20120117” and “20120321”. I need to convert it in a new string with this format: “2012/01/17” and “2012/03/21”. So, there is a way to do this?

I try:

string dateString = string.format("{0:d", "20120321");

and

string dateString = string.format("{0:yyyy/MM/dd", "20120321"); 

and

string dateString = int.Parse("20120321").ToString("yyyy/MM/dd");

I all cases i don’t reach my goal. =/

So, i can i do this?

OBS: There is a way to do that without parse to datetime?

  • 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-31T18:27:59+00:00Added an answer on May 31, 2026 at 6:27 pm

    You have to parse those values in DateTime objects first.

    Example :

    DateTime dt = DateTime.ParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
    var result = dt.ToString("yyyy/MM/dd");
    

    Edit after your comments on other answers:
    if you don’t like parsing because it may throw excepations, you can always use TryParse, like this:

    DateTime dt;
    bool success = DateTime.TryParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
    if (success)
    {
        var result = dt.ToString("yyyy/MM/dd");
    }
    

    Edit 2: Using TryParseExact with multiple formats:

    DateTime dt;
    string[] formats = { "yyyyMMdd", "yyyy" };
    
    bool success = DateTime.TryParseExact("20120321", formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
    if (success)
    {
        var result = dt.ToString("yyyy/MM/dd");
        Console.WriteLine(result);
    }
    

    It will produce 2012/03/21 when using “20120321” as input value, and 2012/01/01 when using 2012 as input value.

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

Sidebar

Related Questions

I have a stringstream where it has many strings inside like this: <A style=FONT-WEIGHT:
I have a big array with many strings like this: $langArr = array( 'welcome'
Assuming I have many strings that are like this: <a id=1.1> <a id=1.1.1> <a
I've got a class with many string arrays. I'd like to have one generic
In code at work, we have many uses of magic strings like the following
I have a huge list containing many strings like: ['xxxx','xx','xy','yy','x',......] Now I am looking
I have strings like this: `(val1, val2, val3)` And I have ANTLR grammar to
Let's say I have text like: <p>There are many people in Asia.</p> I want
I have many classes/methods like this: template<typename CharT, typename TraitsT = std::char_traits<CharT> > struct
Asked many times, but I can't get it to work... I have strings like:

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.