开发者

how to convert datetime to a particular format

private void buildGrid()
{
    this.currentActivities = new XSPIncidentActivityModel.XSPIncidentActivityEntities(BuildEntityConnectionString("XSMDSN")).XSP_IncidentActivity.Where(ia => ia.IncidentID == this.IncidentID).OrderBy(ia=>ia.CompletionDate).ToList();

    foreach (XSPIncidentActivityModel.XSP_IncidentActivity act in this.currentActivities)
    {
        act.CompletionDate.AddHours(this.TimeZoneOffset); 


        if (act.CompletionDate.IsDaylightSavingTime())
        {
act.CompletionDate.AddHours(-1).ToString("MMM dd, yyyy");
        }
    }

    CommonGrid1.BindDataSource(this.currentActivities);
}

I have to conert the datetime object to a particular format i.e, currentl;y it displays 9/14/2009 12:20:30 PM

i have to display in this manner 9/14/2009 12:20 PM

by converting it to string string s = String.Format("{0:g}", act.CompletionDate.AddHours(this.TimeZoneOff开发者_StackOverflow中文版set)); i can display date time without seconds.

But since the data is coming from sql server i have to dislay the datetime object (act.CompletionDate) in that particular format

i tried this but it isnot working string[] expectedFormats = {"G", "g", "f" ,"F"}; IFormatProvider culture = new CultureInfo("en-GB", true); act.CompletionDate = DateTime.ParseExact(s, expectedFormats, culture, DateTimeStyles.AllowWhiteSpaces);


Some examples that may help

  public static string Change_to_date_yymmdd(DateTime dt)
    {
        //string strTest = stringTest();
        string format = "yyMMdd";
        string date = dt.ToString(format, DateTimeFormatInfo.InvariantInfo);
        return date;
    }

    public static string Change_to_time_24hr60min60sec60ms(DateTime dt)
    {
        string format = "HHmmssff";
        string time = dt.ToString(format, DateTimeFormatInfo.InvariantInfo);
        return time;

    }
    public static string Change_to_rangevalidator(DateTime dt)
    {
        //string strTest = stringTest();
        string format = "yyyy-MM-dd-HH-mm-ss";
        string date = dt.ToString(format, DateTimeFormatInfo.InvariantInfo);
        return date;
    }


If you want to set the culture format for ALL dates (and other locale specific output) then you can do this in the web.config for your entire application:

<globalization uiCulture="en" culture="en-GB" />

See http://msdn.microsoft.com/en-us/library/bz9tc508.aspx


Assuming act.CompletionDate is of type System.DateTime, your code lines which call act.CompletionDate.AddHours are doing nothing, because the AddHours method does not change the instance on which it is invoked. Instead a new DateTime is returned which has the hours added. You can fix this by doing something like:-

act.CompletionDate = act.CompletionDate.AddHours(-1)

Likewise, the calls to ToString also achieve nothing since the ToString method does nothing to the instance on which it is invoked. It returns a new string object, generated based on the value of the DateTime.

Instead, in order to fix your formatting problem, try setting the ItemStyle.DataFormatString for the column in the GridView which displays the date. For an example of how to do this, have a look at the MSDN article GridView Examples for ASP.NET 2.0: Formatting the GridView.


string date = DateTime.Now.ToString("M/d/yyyy hh:mm tt");
// prints in this format 9/14/2009 12:20 PM
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜