Convert String (UTC) to DateTime (EST) [duplicate] - c#

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
I need to convert a value to a C# DateTime.
Data looks like below:
20161021-12:55:16.000
20161021-13:22:09.974
I tried
String dtTime = "20161021-13:22:09.974";
dtTime = dtTime.Replace("-"," ");
DateTime outPut = Convert.ToDateTime(dtTime);
and it throws an error.
Can I get help to convert these samples into a valid date time?
Also these values are in UTC.
I need to convert them to EST.
Appreciate your help.

You need to use DateTime.ParseExact with your specific format.
Check out this: https://msdn.microsoft.com/es-es/library/w2sa9yss(v=vs.110).aspx
For the eastern time conversion, try something like this (replace DateTime.Now with your date):
var timeUtc = DateTime.UtcNow;
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, easternZone);

I would use TryParseExact:
String dtTime = "20161021-13:22:09.974";
DateTime outPut;
if (DateTime.TryParseExact(dtTime, "yyyyMMdd-HH:mm:ss.fff", null, DateTimeStyles.None, out outPut))
{
var est = TimeZoneInfo.ConvertTimeFromUtc(outPut,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
Console.WriteLine(est);
}

Related

How can i convert a string to date time in c#? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 1 year ago.
I have a string returned from an api call .
The string is : 11/25/2021 05:20:21
Now that is a mm/dd/yyyy hh:mm:ss
I want to compare it to the datetime 2021-11-25 05:03:17.667
I do the following:
var dt1="11/25/2021 05:20:21";
var dt2= "2021-11-25 05:03:17.667"
DateTime date1 = Convert.ToDateTime(dt1);
DateTime date2 = Convert.ToDateTime(dt2);
int result = DateTime.Compare(date1, date2);
if (result < 0)
{
//d1 is earlier than d2.
}
but when i do the call DateTime date2 = Convert.ToDateTime(dt2); it gives an error , string is not in the correct format ?
You should probably use DateTime.ParseExact, which allows you to provide an excat format to parse your string.

how to convert date time to only time in c#

i want to convert datetime format to only time format.
data type - string
op- 5/31/2017 8:43:20 PM
req op - 8:43 (in 24 hours format)
code
TankDetailsModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(item.timestamp, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
TankDetailsModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(item.timestamp, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString("HH:mm");
this might help you
You can use .ToString ("HH:mm")
You can get it like this.
DateTime dt = DateTime.Now;
var x = dt.ToString("HH:mm");
Hope this will work.
Use this code:
string myDateString = "2/17/2011 6:46:01 PM";
DateTime datetime = DateTime.Parse(myDateString);
string timeString = datetime.ToShortTimeString();
Console.WriteLine(timeString);

how to change date format to mm/dd/yyyy to dd/mm/yyyy in c# [duplicate]

This question already has answers here:
How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?
(4 answers)
Closed 5 years ago.
im gettin the date in this format "3/16/2017 10:53:44 PM", how to get date in this format "16/3/2017 10:53:44 PM"
sample data
"timestamp":"2017-03-16T17:23:44.860Z"
desired op
"16/3/2017 10:53:44 PM"
code
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(bin.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
data type
public String UpdatedTime { get; set; }
Or Try to parse the datetime.
For example
DateTime dateformat = DateTime.ParseExact(Convert.ToDateTime(bin.timestamp), "dd/M/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(dateformat,TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
You need to convert it to string:
binModel.UpdatedTime = bin.timestamp.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture));
For more information: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
A DateTime has no display format.
Only string represnetations of DateTime does.
You can specify the desired format as a parameter in one of the the ToString method overloads of the DateTime struct:
var s = DateTime.Now.ToString("dd/M/yyyy hh:mm:ss tt");
Now s contains the folowing string: "23/3/2017 09:01:32 AM" (my local time)
var currentDate = DateTime.Now.ToString("dd/MM/yyyy");

C# casting String to DateTime [duplicate]

This question already has an answer here:
Parsing ISO 8601 with timezone to .NET datetime
(1 answer)
Closed 9 years ago.
I want to cast in C# a String to DateTime.
My String contains: String input = "2012-07-31T00:00:00.000+0200"
and i used the following pattern: String datePattern = "yyyy-MM-dd%HH:mm:ss.fffz";
MyDateTime myDate = new DateTime();
MyDateTime myDate = DateTime.ParseExact(input, datePattern, null);
And i am getting the following error: String was not recognized as a valid DateTime.
Was pretty sure, because i am not sure how to solve this 'T'and which Timezone i should use.
There are three variants of it at the msdn side.
Which one i need to use, or can i create my own one?
Any suggestions?
You can use The "K" Custom Format Specifier with "yyyy-MM-ddTHH:mm:ss.FFFK" format like;
string s = "2012-07-31T00:00:00.000+0200";
var date = DateTime.ParseExact(s, "yyyy-MM-dd'T'HH:mm:ss.FFFK", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be;
7/30/2012 10:00:00 PM
Here a demonstration.
For more information take a look at;
Custom Date and Time Format Strings

Convert a string (format: yyyyymmddhhmmss) to datetime [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert string with unusual format into datetime
How can I convert a string to DateTime in c#? example:
string s = "20070406000000";
How can I convert that string into a DateTime?
Use the ParseExact or TryParseExact method:
DateTime t = DateTime.ParseExact("20070406000000", "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
This CodeProject article explains how to do this.
String MyString = "1999-09-01 21:34 PM";
DateTime MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", CultureInfo.InvariantCulture);
Use DateTime.ParseExact()
CultureInfo provider = CultureInfo.InvariantCulture;
var date = DateTime.ParseExact(theDateString, "yyyyMMddHHmmss", provider);
You will need to use the DateTime.ParseExact function to tell it what the format of the string is so it knows how to convert it.
string strDate = "20070406000000";
string strDateTimeFormat = "yyyyMMddHHmmss";
DateTime objDate = DateTime.ParseExact(strDate, strDateTimeFormat, DateTimeFormatInfo.InvariantInfo);

Categories

Resources