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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:38:07+00:00 2026-05-30T08:38:07+00:00

In my database I have a field date of type varchar where the date

  • 0

In my database I have a field date of type varchar where the date is stored in the following format yyyyMMddhhmm, with no spaces or other characters separating them.

Now I need to compare this date with a C# DateTime, therefore I need to convert the string into DateTime. The most logical way that I can think of is to extract from the variable date the sub-string related to year, month and day and create a new DateTime object:

var year = Convert.ToInt32(date.Substring(0, 4));

var month = Convert.ToInt32(date.Substring(4, 2));

var day = Convert.ToInt32(date.Substring(6, 2));

DateTime dateToCompare = new DateTime(year, month, day);

Is there any available C# method that allows me to perform this conversion without writing all this code?

  • 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-30T08:38:09+00:00Added an answer on May 30, 2026 at 8:38 am

    Absolutely – use DateTime.ParseExact:

    DateTime parsed = DateTime.ParseExact(text, "yyyyMMddHHmm",
                                          CultureInfo.InvariantCulture);
    

    Note the HH for 24-hour instead of hh for 12-hour format.

    Alternatively, you could use DateTime.TryParseExact, which returns a true/false value to indicate whether or not the parsing was successful. If you’re fully expecting all the data to be valid, and it’s reasonable for an exception to be thrown otherwise, then DateTime.ParseExact is fine.

    As a very different alternative, you could use Noda Time:

    // Do this once...
    var pattern = LocalDateTimePattern.CreateWithInvariantInfo("yyyyMMddHHmm");
    
    // Do this repeatedly...
    var parseResult = pattern.Parse(text);
    if (parseResult.Success)
    {
        LocalDateTime value = parseResult.Value;
        // Use the value...
    }
    else
    {
        // Error... 
    }
    

    Or for the “just throw an exception” behaviour, just use parseResult.Value unconditionally.

    EDIT: As an aside, is there any reason why you’re storing dates in a varchar column? Can you fix your schema instead?

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

Sidebar

Related Questions

In my database, I have a date field of type varchar(8) that I am
I have a table with a field of type date within a MySQL database.
I have a Timestamp field in a WebSQL database that is stored like: /Date(1340420400000)/
I have a sqlite3 database with a datetime field, the format of the date/time
I have a database table with date field whose data type is also date
I have a date field in the database table of this format 2012-02-1.i need
In mysql database, I have a field called date of date type. When I
I have following database schema for tabel tblpers Field Type Null Key Default Extra
I have Database with date field. I see the time like: 1900-01-01 13:38:00.000 How
I have a huge database (800MB) which consists of a field called 'Date Last

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.