转换日期时间,特定的格式在C#

问题描述:

我想转换日期时间,以指定格式,转换日期时间,特定的格式在C#

Wed Aug 01 2012 14:37:50 GMT+0530 (India Standard Time) 

其实我想显示的网页上使用jQuery的定时器。所以我尝试了一些我知道的格式。并发现一些从http://www.dotnetperls.com/datetime-format但他们都没有返回我需要的结果。其实我必须从服务器传递时间所以我已经尝试了下面的代码。 代码隐藏

protected void Button3_Click(object sender, EventArgs e) 
{ 
    //string hello = DateTime.UtcNow.ToString(); 
    string hello = String.Format("{0:F}", DateTime.UtcNow); 

    DateTime.UtcNow.ToString(""); 
    ScriptManager.RegisterStartupScript(this, this.GetType(), "hdrEmpty1", "show(" + hello + ")", true); 
} 

jQuery的

function show(datetime) { 
     alert(datetime); 
     var Digital = datetime //new Date() 
     var hours = Digital.getHours() 
     var minutes = Digital.getMinutes() 
     var seconds = Digital.getSeconds() 
     var dn = "AM" 
     if (hours > 12) { 
      dn = "PM" 
      hours = hours - 12 
     } 
     if (hours == 0) 
      hours = 12 
     if (minutes <= 9) 
      minutes = "0" + minutes 
     if (seconds <= 9) 
      seconds = "0" + seconds 
     document.getElementById('<%= Label1.ClientID %>').innerHTML = hours + ":" + minutes + ":" + seconds + " " + dn 
     setTimeout("show()", 1000) 
    } 
+0

我猜datetime字符串GMT选项不可用。 – 2012-08-01 09:13:02

+0

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx – 2012-08-01 09:13:29

+2

一个简单的谷歌搜索会给你很多关于这方面的信息。例如,这些例子,应该让你开始:http://www.csharp-examples.net/string-format-datetime/ – Kjartan 2012-08-01 09:14:39

您可以使用date.ToString("format")做到这一点。微软提供了一个full reference关于如何以你想要的方式格式化你的日期。

编辑:

也许没有现成的格式正是您的格式相匹配,但您可以根据上述文献提供的格式说明结合你自己的。

// This will output something like Wed Aug 01 2012 
date.ToString("ddd MMM dd yyyy"); 

我相信你可以按照相同的模式完成其余的你自己。

+0

我没有得到这样的格式,返回“Wed Aug 01 2012 14:37:50 GMT + 0530(印度标准时间)” – 2012-08-01 09:37:14

+1

@raman我添加了一个例子让你开始,我相信你可以遵循这种模式自己完成休息。 – 2012-08-01 09:49:45

您可以使用String.Format()并指定您自己的自定义格式 - ddd mmm dd yyyy。亲自尝试一下,探索更多。

+0

我没有得到这样的格式,返回“Wed Aug 01 2012 14:37:50 GMT + 0530(印度标准时间)” – 2012-08-01 09:36:58

+1

是格林威治标准时间+530(印度......)是固定的还是会根据时区变化? – Umesh 2012-08-01 09:53:47

没有理由已提供,你不能工作了这一点为自己,而是为了得到一个字符串的阅读信息:

"Wed Aug 01 2012 14:37:50 GMT+0530 (India Standard Time)" 

那么你想要的是得到的日期和时间最后加上“GMT + 0530(印度标准时间)”的正确格式(“Wed Aug 01 2012 14:37:50”位)。假设那个位当然总是一样的。

那么你需要的代码是:

string string_name = (date.ToString("ddd MMM dd yyyy HH:mm:ss") + "GMT+0530 \(India Standard Time\)"); 
//string_name will be in the form "Wed Aug 01 2012 14:37:50 GMT+0530 (India Standard Time)" 

但是,正如我所说,这东西你应该能够制定出自己使用提供的参考。

如果你不想硬编码GMT偏移,不能依靠当地的时间是印度标准时间,你可以拉从TimeZoneInfo这样的信息:

// get UTC from local time 
var today = DateTime.Now.ToUniversalTime(); 
// get IST from UTC 
var ist = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(today, "UTC", "India Standard Time"); 
// find the IST TimeZone 
var tzi = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); 
// get the UTC offset TimeSpan 
var offset = tzi.GetUtcOffset(today); 
// determine the TimeSpan sign 
var sign = offset.Ticks < 0 ? "-" : "+"; 
// use a custom format string 
var formatted = string.Format(CultureInfo.InvariantCulture, "{0:ddd MMM HH:mm:ss} GMT{1}{2:hhmm}", today, sign, offset); 

试试这个以日期时间转换为印度的日期格式在c#

IFormatProvider culture = new System.Globalization.CultureInfo("hi-IN", true); 
     DateTime dt2 = DateTime.Parse(txtStatus.Text, culture, System.Globalization.DateTimeStyles.AssumeLocal);