Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting date format in C# identical to javascript date function

Tags:

javascript

c#

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...

like image 896
mike44 Avatar asked Mar 03 '26 03:03

mike44


2 Answers

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"

like image 175
Patrick D'Souza Avatar answered Mar 04 '26 16:03

Patrick D'Souza


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)

like image 38
stevehipwell Avatar answered Mar 04 '26 15:03

stevehipwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!