3

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

1
  • I've updated my answer to give you the exact string that JavaScript gives you including the time zone information. Commented Mar 12, 2013 at 8:41

7 Answers 7

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"

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

1

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"))

Comments

1
string.Format("{0:ddd MMM dd yyyy hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))

Comments

1

You format string is wrong. Should be something like:

"{0:ddd MMM dd yyyy hh:mm:ss} GMT{1}"

Comments

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

Comments

0

Try This Code>>

DateTime dt = DateTime.Now;

String.Format("{0:dd-MM-yyyy}", dt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.