Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extract Date part and pass only Date

Extract Date part and pass only Date

Scheduled Pinned Locked Moved C#
databasehelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meeram39
    wrote on last edited by
    #1

    I am running into a peculiar problem. I want to extract only date part from DateTime and pass the Date as DateTime datatype to stored procedure. I tried with the following code:

    DateTime datetime = DateTime.Now.Date;
    Console.WriteLine("Date is : " + datetime);

    This is always returns with date and time as

    4/27/2013 12:00:00 AM

    . I want only date, no time and should pass this as Date Datatype. Even though I tried with lots of examples, I am able to do it if I convert to string which is not possible. Is there any way which I can achieve this? Am I doing something wrong here?

    D J 2 Replies Last reply
    0
    • M meeram39

      I am running into a peculiar problem. I want to extract only date part from DateTime and pass the Date as DateTime datatype to stored procedure. I tried with the following code:

      DateTime datetime = DateTime.Now.Date;
      Console.WriteLine("Date is : " + datetime);

      This is always returns with date and time as

      4/27/2013 12:00:00 AM

      . I want only date, no time and should pass this as Date Datatype. Even though I tried with lots of examples, I am able to do it if I convert to string which is not possible. Is there any way which I can achieve this? Am I doing something wrong here?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Look at the name of the structure you're using: DateTime The time component is not optional there. It will always have a time because the value for 12:00 AM is 0. There is no Date structure or class in .NET. If you want to pass the date to a stored procedure, pass in a DateTime to a parameter of a parameterized query. When SQL evaluates a date, the time is still there even though it's not specified. Guess what time it is if it's not specified? Yeah, 12:00 AM. You're basically trying to do something that you don't really need to do at all. If you pass a DateTime into a, for example, SqlParameter object and the parameter type is defined as an SqlDbType.Date, the conversion is done automatically.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • M meeram39

        I am running into a peculiar problem. I want to extract only date part from DateTime and pass the Date as DateTime datatype to stored procedure. I tried with the following code:

        DateTime datetime = DateTime.Now.Date;
        Console.WriteLine("Date is : " + datetime);

        This is always returns with date and time as

        4/27/2013 12:00:00 AM

        . I want only date, no time and should pass this as Date Datatype. Even though I tried with lots of examples, I am able to do it if I convert to string which is not possible. Is there any way which I can achieve this? Am I doing something wrong here?

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Besides the prior comment you should also be aware that MS SQL Server only has one data type 'Date' which only holds a date component. If the database has a different data type then it will ALWAYS have a time component regardless of what you want. You might treat it like it doesn't, with care, but you better understand exactly how values go into and what impact timezones might have.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups