I'm assigning the date to the variable in javascript.
var myDate = new Date(y, m, 1)
So I get the date in myDate
as: Fri Mar 01 2013 00:00:00 GMT+0530 (India Standard Time)
I need to format the date string in C sharp in the same manner.
I tried something like this:
string.Format("{0:yyyy-MM-dd hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))
It gives me: "2013-03-12 01:31:49 GMT +05:30"
So it's not the exact format I want. Any help...
I'm assigning the date to the variable in javascript.
var myDate = new Date(y, m, 1)
So I get the date in myDate
as: Fri Mar 01 2013 00:00:00 GMT+0530 (India Standard Time)
I need to format the date string in C sharp in the same manner.
I tried something like this:
string.Format("{0:yyyy-MM-dd hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))
It gives me: "2013-03-12 01:31:49 GMT +05:30"
So it's not the exact format I want. Any help...
Share Improve this question edited Mar 12, 2013 at 8:21 Cris 13.4k5 gold badges36 silver badges51 bronze badges asked Mar 12, 2013 at 8:15 mike44mike44 8125 gold badges15 silver badges38 bronze badges 1- I've updated my answer to give you the exact string that JavaScript gives you including the time zone information. – stevehipwell Commented Mar 12, 2013 at 8:41
7 Answers
Reset to default 2This should work
System.DateTime.Now.ToString("ddd MMM dd yyyy HH:mm:ss \"GMT\"K")
returns "Tue Mar 12 2013 14:01:38 GMT+05:30"
What you want is the following which will give you exactly the same as JavaScript!
string.Format("{0:ddd MMM dd yyyy hh:mm:ss \"GMT\"K} ({1})", dt.ToLocalTime(), TimeZoneInfo.Local.StandardName)
There's probably a more proper way, but in this case the initial part of your format string is just off:
ddd MMM dd yyyy
string.Format("{0:ddd MMM dd yyyy hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))
string.Format("{0:ddd MMM dd yyyy hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))
You format string is wrong. Should be something like:
"{0:ddd MMM dd yyyy hh:mm:ss} GMT{1}"
Why not just use
dt.ToString("ddd MMM dd yyyy HH':'mm':'ss 'GMT'K");
Should give you
Fri Mar 01 2013 00:00:00 GMT+0530
Try This Code>>
DateTime dt = DateTime.Now;
String.Format("{0:dd-MM-yyyy}", dt);