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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:32:40+00:00 2026-06-10T04:32:40+00:00

Possible Duplicate: How to convert DateTime object to dd/mm/yyyy in C#? I’m new to

  • 0

Possible Duplicate:
How to convert DateTime object to dd/mm/yyyy in C#?

I’m new to c# and was hoping someone could help me clean up some code.

I have the following method which converts a DateTime to a custom Event string e.g. 30th of Jan 2012 is converted to 201201 (ignores the day)

public ConvertToEventDate(DateTime date)
{   
    var year = date.Year.ToString();
    var month = date.Month.ToString();
    month = month.Length == 2 ? month : "0" + month;
    return year + month;
}

I was wondering if there is a better way of doing this conversion.

  • 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-06-10T04:32:41+00:00Added an answer on June 10, 2026 at 4:32 am

    I’d do this;

    public string ConvertToEventDate(DateTime date)
    {   
        return date.ToString("yyyyMM");
    }
    

    you can also put this into an Extension method like this;

    public static class ExtenstionMethods
    {
        public static string ToEventDate(this DateTime date)
        {
            return date.ToString("yyyyMM");
        }
    }
    

    and then call it ike this;

    DateTime date = new DateTime(2012, 30, 1);
    
        date.ToEventDate();
    

    as opposed to this;

    ConvertToEventDate(date);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Convert some code from C++ to C I've got some code that
Possible Duplicate: Convert Date String to DateTime Object in Python Is there an easy
Possible Duplicate: how to convert java string to Date object In my Java code
Possible Duplicate: Convert a datetime.date object into a datetime.datetime object with zeros for any
Possible Duplicate: Convert NSString of a date to an NSDate I need some help
Possible Duplicate: How to convert yyyy-MM-dd HH:mm:ss to “15th Apr 2010” using PHP I
Possible Duplicate: Converting .NET DateTime to JSON How can I convert a date value
Possible Duplicate: How to convert from int to string in objective c: example code…
Possible Duplicate: Convert string to DateTime in c# A question I got a string
Possible Duplicate: How can I convert datetime microformat to local time in javascript? Im

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.