admin管理员组文章数量:1356808
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);
本文标签: Getting date format in C identical to javascript date functionStack Overflow
版权声明:本文标题:Getting date format in C# identical to javascript date function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744008610a2575151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论