简体   繁体   中英

How to get only the date part from Different Time Zone C#

Trying to get the Date of a different country (time zone) and then display it. I'm getting the time and date. Just need the date

var info = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    var dateTime = TimeZoneInfo.ConvertTime(DateTime.Now.Date, info);

    MessageBox.Show(dateTime.Date.ToString());

Need 05/05/2015 but not the time

You can Use directly

MessageBox.Show(dateTime.ToShortDateString());

or else you can customize it

MessageBox.Show(dateTime.ToString("dd/MM/yyyy"));

You can get date only by using ToShortDateString()

var info = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var dateTime = TimeZoneInfo.ConvertTime(DateTime.Now.Date, info);

MessageBox.Show(dateTime.Date.ToShortDateString());

Or you can directly use

MessageBox.Show(dateTime.ToShortDateString());

You need to use a format in any case, if you want to specify the culture for a specific country you can do it, or use one of the method of the class DateTime(ToShortDateString(), ToLongDateString(), etc)

var ci = new CultureInfo("en-US"); 

or var ci = CultureInfo.InvariantCulture; explore CultureInfo for more information

Then use the ToString method to obtain your date in the specific format, see that format does not include hour or minutes.

dateTime.ToString("MM/dd/yyyy", provider)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM