ASP.NET - Localtime format of DateTime

Asked By Bichel A on 20-Jun-11 11:12 PM
I have a datagridview that displays datetime values from dataset in universal time. How can I set
this datagridview to displays values in local time? Any property to change?

Thanks
Venkat K replied to Bichel A on 20-Jun-11 11:17 PM
Set the culture to the required one and displya the time as per your need:

CultureInfo us = new CultureInfo("en-US");
string shortUsDateFormatString = us.DateTimeFormat.ShortDatePattern;
string shortUsTimeFormatString = us.DateTimeFormat.ShortTimePattern;
 
CultureInfo uk = new CultureInfo("en-UK");
string shortUkDateFormatString = uk.DateTimeFormat.ShortDatePattern;
string shortUkTimeFormatString = uk.DateTimeFormat.ShortTimePattern;


Thanks
TSN ... replied to Bichel A on 20-Jun-11 11:37 PM
hi..

use the below line of code to format date in gridview...

<asp id="GridView3" runat="server" :GridView>
<columns>
<asp headertext="CreationDate" :TemplateField>
<edititemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp>
</edititemtemplate>
<itemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>;
</asp>
</itemtemplate>
</asp>
</columns>
</asp>
 

or you can also use...

<asp id="GridView1" runat="server" :GridView>
<columns>
<asp headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" :BoundField HtmlEncode="false" />
</columns>


hope this helps you...
Jitendra Faye replied to Bichel A on 21-Jun-11 12:07 AM

For displaying Different date format you can give dateFormatString in GridView Fileds.

Like -

If date in your GridView Column, first you have to convert your date to short date format.

For converting your date to this format you have to use DataFormatString Property of Bound Field.
 

Sample -

<asp:BoundField  DataField="OrderDate" DataFormatString="{0:d}" HeaderText="Date"
</asp:BoundField>

Use this code and let me .

Anoop S replied to Bichel A on 21-Jun-11 12:45 AM

did you try this:


Dim italy As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
 
Dim userInput As Date = Date.Parse("21-mar-2011 00:00")
 
Dim utcDate As Date = TimeZoneInfo.ConvertTimeToUtc(userInput, italy)
 
Dim italyDate As Date = TimeZoneInfo.ConvertTimeFromUtc(utcDate, italy)
 
Response.Write(italyDate)

Mihir Soni replied to Bichel A on 21-Jun-11 01:01 AM
Hello,

For that you can use DataFormatString to format your data at the client side.

It's actually very useful when you wants to display data on the bases of client.

Assuming that you have one bound column which should be displayed in DD-MM-YYYY format below is an example for the same.

<asp:BoundField DataField="BirthDates"
HeaderText="Birth Dates"
DataFormatString= "{dd-MM-yyyy}" />

Above will display dates in DD-MM-YYYY format.

Hope this helps you

Thank you.
Kirtan Patel replied to Bichel A on 21-Jun-11 04:41 AM
change the thread cultured info and your dates will be shown as your culture you can initialize the culture info before page loads so it will show all the date format etc according to set culture info do it like below
<%@ Page Language="C#" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
protected override void InitializeCulture()
{
  if (Request.Form["ListBox1"] != null)
  {
    String selectedLanguage = Request.Form["ListBox1"];
    UICulture = selectedLanguage ;
    Culture = selectedLanguage ;
    Thread.CurrentThread.CurrentCulture = 
      CultureInfo.CreateSpecificCulture(selectedLanguage);
    Thread.CurrentThread.CurrentUICulture = new 
      CultureInfo(selectedLanguage);
  }
  base.InitializeCulture();
}
</script>
Thanks