最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting date format in C# identical to javascript date function - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

7 Answers 7

Reset to default 2

This 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);
发布评论

评论列表(0)

  1. 暂无评论