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. LINQ
  4. Converting Long Date To Short Date Format in Linq Query

Converting Long Date To Short Date Format in Linq Query

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinq
22 Posts 3 Posters 2 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.
  • J J4amieC

    Sanket.Patil wrote:

    So If There is any other method For This Please Reply.

    Make sure those 2 fields are of type DATETIME when doing your SQL select.

    S Offline
    S Offline
    Sanket Patil
    wrote on last edited by
    #13

    Hi J4amieC, Here is my code to review :-

    private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
    {
    try
    {
    cmbFinacialYear.DataSource = null;
    cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
    cmbFinacialYear.DisplayMember = "Sdate";
    cmbFinacialYear.ValueMember = "Code";
    cmbFinacialYear.SelectedIndex = -1;
    }
    catch (Exception ex)
    {
    objcon.WriteLog("Login Master", "btnLogin_Click", ex);
    }
    }

    Hope you will find the better Solution From these. Thanks for the Help.... Thanks & Regards -- --- ---- Sanket Patil.

    .

    J 1 Reply Last reply
    0
    • S Sanket Patil

      Hi Richard, Here is my Code to Review :-

      private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
      {
      try
      {
      cmbFinacialYear.DataSource = null;
      cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
      cmbFinacialYear.DisplayMember = "Sdate";
      cmbFinacialYear.ValueMember = "Code";
      cmbFinacialYear.SelectedIndex = -1;
      }
      catch (Exception ex)
      {
      objcon.WriteLog("Login Master", "btnLogin_Click", ex);
      }
      }

      Hope You will get better Solution From These. Thanks For Help..... Thanks & Regard -- --- ---- Sanket Patil

      .

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #14

      You have posted the original code twice and in both cases you are creating Sdate as a concatenation of various fields including n.CM_ST_DATE. However this latter field will be returning its default ToString() method rather than the formatted version that you require. Try n.CM_ST_DATE.ToString("D"), or some alternate DateTime format string to get the date only. [EDIT]above should read n.CM_ST_DATE.ToString("d").[/EDIT]

      modified on Tuesday, October 6, 2009 8:41 AM

      S 1 Reply Last reply
      0
      • L Lost User

        You have posted the original code twice and in both cases you are creating Sdate as a concatenation of various fields including n.CM_ST_DATE. However this latter field will be returning its default ToString() method rather than the formatted version that you require. Try n.CM_ST_DATE.ToString("D"), or some alternate DateTime format string to get the date only. [EDIT]above should read n.CM_ST_DATE.ToString("d").[/EDIT]

        modified on Tuesday, October 6, 2009 8:41 AM

        S Offline
        S Offline
        Sanket Patil
        wrote on last edited by
        #15

        Hi Richard, Thanks a lot for your help. The good news is i found the solution but not satisfied. Still the solution for now is best.Instead of getting result in "dd/MMM/yyyy" i am getting the result as "d/MM/yyyy". The Code I used for this is as Follows:- Previous Code :-

        private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
        {
        try
        {
        cmbFinacialYear.DataSource = null;
        cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
        cmbFinacialYear.DisplayMember = "Sdate";
        cmbFinacialYear.ValueMember = "Code";
        cmbFinacialYear.SelectedIndex = -1;
        }
        catch (Exception ex)
        {
        objcon.WriteLog("Login Master", "btnLogin_Click", ex);
        }
        }

        And The Code I Made Changes is :-

        private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
        {
        try
        {
        cmbFinacialYear.DataSource = null;
        cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE.Day + "/" + n.CM_ST_DATE.Month + "/" + n.CM_ST_DATE.Year + " - To : " + n.CM_END_DATE.Day + "/" + n.CM_END_DATE.Month + "/" + n.CM_END_DATE.Year, Code = n.CM_CODE });
        cmbFinacialYear.DisplayMember = "Sdate";
        cmbFinacialYear.ValueMember = "Code";
        cmbFinacialYear.SelectedIndex = -1;
        }
        catch (Exception ex)
        {
        objcon.WriteLog("Login Master", "btnLogin_Click", ex);
        }
        }

        Hope You will be satisfy From this. Thanks For Help Thanks & Regards -- --- ---- Sanket. Patil

        .

        L 1 Reply Last reply
        0
        • S Sanket Patil

          Hi Richard, Thanks a lot for your help. The good news is i found the solution but not satisfied. Still the solution for now is best.Instead of getting result in "dd/MMM/yyyy" i am getting the result as "d/MM/yyyy". The Code I used for this is as Follows:- Previous Code :-

          private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
          {
          try
          {
          cmbFinacialYear.DataSource = null;
          cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
          cmbFinacialYear.DisplayMember = "Sdate";
          cmbFinacialYear.ValueMember = "Code";
          cmbFinacialYear.SelectedIndex = -1;
          }
          catch (Exception ex)
          {
          objcon.WriteLog("Login Master", "btnLogin_Click", ex);
          }
          }

          And The Code I Made Changes is :-

          private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
          {
          try
          {
          cmbFinacialYear.DataSource = null;
          cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE.Day + "/" + n.CM_ST_DATE.Month + "/" + n.CM_ST_DATE.Year + " - To : " + n.CM_END_DATE.Day + "/" + n.CM_END_DATE.Month + "/" + n.CM_END_DATE.Year, Code = n.CM_CODE });
          cmbFinacialYear.DisplayMember = "Sdate";
          cmbFinacialYear.ValueMember = "Code";
          cmbFinacialYear.SelectedIndex = -1;
          }
          catch (Exception ex)
          {
          objcon.WriteLog("Login Master", "btnLogin_Click", ex);
          }
          }

          Hope You will be satisfy From this. Thanks For Help Thanks & Regards -- --- ---- Sanket. Patil

          .

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #16

          Well I don't know why you are making things more difficult for yourself. I suggested that you use n.CM_ST_DATE.ToString(formatstring) (where formatstring is a standard or custom date format string), which will provide everything and anything you need. Take a look at the documentation here on MSDN[^] for all the possibilities.

          J 1 Reply Last reply
          0
          • L Lost User

            Well I don't know why you are making things more difficult for yourself. I suggested that you use n.CM_ST_DATE.ToString(formatstring) (where formatstring is a standard or custom date format string), which will provide everything and anything you need. Take a look at the documentation here on MSDN[^] for all the possibilities.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #17

            Richard MacCutchan wrote:

            I suggested that you use n.CM_ST_DATE.ToString(formatstring)

            As did I, more than 24 hours ago. This person is incapable of reading documentation or taking advice.. therefore I gave up. I suggest you do too.

            L 1 Reply Last reply
            0
            • J J4amieC

              Richard MacCutchan wrote:

              I suggested that you use n.CM_ST_DATE.ToString(formatstring)

              As did I, more than 24 hours ago. This person is incapable of reading documentation or taking advice.. therefore I gave up. I suggest you do too.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #18

              J4amieC wrote:

              therefore I gave up. I suggest you do too.

              I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.

              J 2 Replies Last reply
              0
              • S Sanket Patil

                Hi J4amieC, Here is my code to review :-

                private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                {
                try
                {
                cmbFinacialYear.DataSource = null;
                cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
                cmbFinacialYear.DisplayMember = "Sdate";
                cmbFinacialYear.ValueMember = "Code";
                cmbFinacialYear.SelectedIndex = -1;
                }
                catch (Exception ex)
                {
                objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                }
                }

                Hope you will find the better Solution From these. Thanks for the Help.... Thanks & Regards -- --- ---- Sanket Patil.

                .

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #19

                Change this line:

                cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });

                to

                cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = String.Format("From: {0:dd/MM/yyyy} - To: {1:dd/MM/yyyy}",n.CM_ST_DATE,n.CM_END_DATE), Code = n.CM_CODE });

                Im not going to bother explaining why. I just want this question to die already.

                1 Reply Last reply
                0
                • L Lost User

                  J4amieC wrote:

                  therefore I gave up. I suggest you do too.

                  I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #20

                  You're right. So I took pity, and tried once more for a solution.

                  1 Reply Last reply
                  0
                  • L Lost User

                    J4amieC wrote:

                    therefore I gave up. I suggest you do too.

                    I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #21

                    What a surprise, our OP just doesnt bother to respond to 2 serious answers.

                    L 1 Reply Last reply
                    0
                    • J J4amieC

                      What a surprise, our OP just doesnt bother to respond to 2 serious answers.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #22

                      J4amieC wrote:

                      What a surprise, our OP just doesnt bother to respond to 2 serious answers.

                      Patience brother! ;)

                      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