Author : HASSAN MD TAREQ | Updated : 2020/08/01

DateTime

Conversion to string

String to DateTime

var dt = Convert.ToDateTime("2020/04/27") 

var dt = (DateTime)Convert.ChangeType("2020/12/31", typeof(DateTime))


// DateTime.Parse Syntax
DateTime.Parse(String value)
DateTime.Parse(String value, IFormatProvider provider)
DateTime.Parse(String value, IFormatProvider provider, DateTypeStyles styles)

// DateTime.Parse Example 
string value = "1 January 2019";
CultureInfo provider = new CultureInfo("en-GB");
DateTime.Parse(value, provider, DateTimeStyles.NoCurrentDateDefault););

// DateTime.ParseExact
DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff",
                                       System.Globalization.CultureInfo.InvariantCulture);

Links:

DateTime Format

https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1

Convert to specific timezone

public const string TokyoTimeZoneId = "Tokyo Standard Time";
		
var ModifiedOn = TimeZoneInfo.ConvertTimeFromUtc(dateTime: DateTime.UtcNow,
                                                                destinationTimeZone: TimeZoneInfo.FindSystemTimeZoneById(TokyoTimeZoneId));