I want to convert date time to specify format that is
Wed Aug 01 2012 14:37:50 GMT+0530 (India Standard Time)
Actually i want to display the Timer using Jquery on web page. So i have tried some format i knew. And found some from http://www.dotnetperls.com/datetime-format But none of them return the result what i require. Actually i have to pass the time from server So i have tried the following code.
Code Behind
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)
}
You can use date.ToString("format")
to do that. Microsoft offer a full reference on how to format your dates the way you want.
Edit:
Perhaps there isn't a format ready that exactly matches your format, but you can combine your own based on the format specifiers provided in the above reference.
// This will output something like Wed Aug 01 2012
date.ToString("ddd MMM dd yyyy");
I believe you can follow the same pattern to complete the rest on your own.
You can use String.Format()
and specify your own custom format - ddd mmm dd yyyy
. Try it yourself to explore more.
There's no reason with the information already provided that you can't work this out for yourself, but to get a string reading:
"Wed Aug 01 2012 14:37:50 GMT+0530 (India Standard Time)"
Then what you want is to get the date and time in the correct form (the "Wed Aug 01 2012 14:37:50" bit) with "GMT+0530 (India Standard Time)" added on the end. Assuming that that bit is always the same of course.
So the code you'd need is:
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)"
But, as I said, that's something you should be able to work out yourself using the references provided.
If you do not want to hardcode the GMT offset and cannot rely on the local time being India Standard Time you can pull this information from the 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);
try this to convert datetime to indian date format in c#
IFormatProvider culture = new System.Globalization.CultureInfo("hi-IN", true);
DateTime dt2 = DateTime.Parse(txtStatus.Text, culture, System.Globalization.DateTimeStyles.AssumeLocal);