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. Web Development
  3. ASP.NET
  4. DateFormat problem

DateFormat problem

Scheduled Pinned Locked Moved ASP.NET
databasehelpsql-serversysadminquestion
7 Posts 4 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.
  • C Offline
    C Offline
    Commickey
    wrote on last edited by
    #1

    Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey

    M S F 3 Replies Last reply
    0
    • C Commickey

      Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey

      M Offline
      M Offline
      mehnazash
      wrote on last edited by
      #2

      //Check the third field , this is how u need to write 103 represents some code given by .net string query = "select build_desc,baseline,folder,convert(varchar(10),build_date,103),comment,developed_by,convert(varchar(10),modified_on,103),approval_flag from build_master order by build_date"; Hope this helps Jiny -- modified at 5:59 Thursday 29th June, 2006

      C 1 Reply Last reply
      0
      • C Commickey

        Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey

        S Offline
        S Offline
        Sushant Duggal
        wrote on last edited by
        #3

        Hi, Use string.Format("{o:dd/MM/yyyy}",yourdate); check whether mm or MM Thanks, Sushant Duggal. -- modified at 5:49 Thursday 29th June, 2006

        1 Reply Last reply
        0
        • C Commickey

          Hello, I am using Sql server, I want to display a date on a datagrid with the following details: _ Only the date and not the time _ The format of the date should be dd/mm/dddd I am getting this column from the database and would like to display on a datagrid.... can someone help with the sql statement that will help me in doing this conversion? Thanks alot, Commickey

          F Offline
          F Offline
          Farshid Zavareh
          wrote on last edited by
          #4

          I don't know how you can do date conversion in SQL, but you can use the following code to do this in page code:

          // Language = C#
          // dgrd = DataGrid
          // change cell index to cell index for your datagrid
          
          foreach (DataGridItem item in dgrd.Items)
          {
              DateTime date = DateTime.Parse(item.Cells\[1\].Text);
              item.Cells\[1\].Text = date.ToShortDateString();
          }
          

          This sould work, but if result is not in dd/mm/yyyy format then use the following:

          // Language = C#
          // dgrd = DataGrid
          // change cell index to cell index for your datagrid
          
          foreach (DataGridItem item in dgrd.Items)
          {
              DateTime date = DateTime.Parse(item.Cells\[1\].Text);
              string text = date.Day + "/" + date.Month + "/" +
                  date.Year;
              item.Cells\[1\].Text = text;
          }
          

          Just make sure data type for the column in database is datetime. If it is not, use a try...catch loop when parsing string to prevent error. Regards, lordfkiller

          C 1 Reply Last reply
          0
          • M mehnazash

            //Check the third field , this is how u need to write 103 represents some code given by .net string query = "select build_desc,baseline,folder,convert(varchar(10),build_date,103),comment,developed_by,convert(varchar(10),modified_on,103),approval_flag from build_master order by build_date"; Hope this helps Jiny -- modified at 5:59 Thursday 29th June, 2006

            C Offline
            C Offline
            Commickey
            wrote on last edited by
            #5

            Yes... thanks... it solved my problem....

            1 Reply Last reply
            0
            • F Farshid Zavareh

              I don't know how you can do date conversion in SQL, but you can use the following code to do this in page code:

              // Language = C#
              // dgrd = DataGrid
              // change cell index to cell index for your datagrid
              
              foreach (DataGridItem item in dgrd.Items)
              {
                  DateTime date = DateTime.Parse(item.Cells\[1\].Text);
                  item.Cells\[1\].Text = date.ToShortDateString();
              }
              

              This sould work, but if result is not in dd/mm/yyyy format then use the following:

              // Language = C#
              // dgrd = DataGrid
              // change cell index to cell index for your datagrid
              
              foreach (DataGridItem item in dgrd.Items)
              {
                  DateTime date = DateTime.Parse(item.Cells\[1\].Text);
                  string text = date.Day + "/" + date.Month + "/" +
                      date.Year;
                  item.Cells\[1\].Text = text;
              }
              

              Just make sure data type for the column in database is datetime. If it is not, use a try...catch loop when parsing string to prevent error. Regards, lordfkiller

              C Offline
              C Offline
              Commickey
              wrote on last edited by
              #6

              Hello, where should I implement this? in the itemdatabound?

              F 1 Reply Last reply
              0
              • C Commickey

                Hello, where should I implement this? in the itemdatabound?

                F Offline
                F Offline
                Farshid Zavareh
                wrote on last edited by
                #7

                This is what I usually do: The code should be executed after DataGrid is data bound(DataGrid.DataBind() is called). So that it can edit DataGrid items. In post backs, you don't need to data bind DataGrid, because it is already data bound (in load), but you may need to run converting code in post backs, too. To do so, data bind DataGrid in page_load (use if(!IsPostBack)). Place code for modifying DataGrid in a separate method and call it in page_load (outside if(!IsPostBack)). If you are using ASP.NET 2, note that when you do data binding using a DataSource, you still need to call DataGrid.DataBind() in page_load. Because this method is called after page_load in page life cycle and you need to work on DataGrid in page_load, you must call it explicitly. But using ItemDataBound, it is much better and easier:

                protected void dgrdWebSites\_ItemDataBound(object s, DataGridItemEventArgs e)
                {
                    DateTime date = DateTime.Parse(e.Item.Cells\[1\].Text);
                    string text = date.Day + "/" + date.Month + "/" +
                        date.Year;
                    e.Item.Cells\[1\].Text = text;
                }
                

                Nothing more is needed! Let me know if you need more explanation. Best regards, lordfkiller

                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