Anyone knows an equivalent class of Java's SimpleDateFormat in C#? I wonder if i can convert the following java code using Custom Date and Time Format Strings in C#
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
TimeZone tz = TimeZone.getDefault();
format.setTimeZone(TimeZone.getTimeZone("GMT"));
str = format.format(new Date());
You can use DateTime
's ToString()
method and specify the custom format that you want the DateTime
to be output in. In this case:
// US culture
var usCulture = new CultureInfo("en-US");
// Get current UTC time.
var utcDate = DateTime.UtcNow;
// Change time to match GMT + 1.
var gmt1Date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDate, "W. Europe Standard Time");
// Output the GMT+1 time in our specified format using the US-culture.
var str = gmt1Date.ToString("ddd, dd MMM yyyy HH:mm:ss z", usCulture);
Please note that .NET's equivalent of EEE is ddd.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With