Format Date Field in Web Front End

  • Hi. I have a Web Page that displays data from a SQL Database in the format:2/15/2008 12:00:00 AM

    I would like to display as: 15/02/2008

    I read that I could use the following:

    String.Format("{0} {1} {2}", Date.Now.Day, MonthName(Date.Now.Month, True), Date.Now.Year);

    However I am unsure where to insert the above code? In the SQL Query / or embed in the code on the web page? SQL Query:

    SELECT OrderId, DueDate,DocumentName

    FROM Orders

    LEFT JOIN dbo.Documents

    ON Orders.Product = Documents.Identifier

    [/Code]

    Thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

  • Phil,

    That String.Format you were given is for the front-end code. That should work well enough. Or you could opt to format it in SQL Server if you wanted to.

    SELECT OrderId, convert(varchar, DueDate, 103) ,DocumentName

    FROM Orders

    LEFT JOIN dbo.Documents

    ON Orders.Product = Documents.Identifier

    The number 103 in the convert function, which relates to the style, is what tells SQL Server to convert it to dd/mm/yyyy format. There are various other styles (see books online) that you could convert it to.

    Which method you choose is more a matter of preference really.

  • Thanks Karl. I just managed to format via Visual Web Developer as follows:

    Grid View / Edit Column / DataFormatString = {0:dd/MM/yyyy}

    I will run the SQL version as I would prefer to do it all in the code.

    Many Thanks,

    Phil.

    -------------------------------------------------------------------------------------
    A neutron walks into a bar. "I'd like a beer" he says. The bartender promptly serves up a beer. "How much will that be?" asks the neutron. "For you?" replies the bartender, "no charge."

    Two hydrogen atoms walk into a bar. One says, 'I think I've lost an electron.' The other says 'Are you sure?' The first says, 'Yes, I'm positive... '

    Tommy Cooper

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply