How to convert a UTC Datetime to Local Datetime? [duplicate] - c#

This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 9 years ago.
I need to convert a utc datetime to a local datetime.
it should look something like this:
private DateTime localdate (DateTime UTC_DateTime)
{
DateTime local = new DateTime();
local = UTC_DateTime ..... ???
return local;
}
Thanks in advance for your Help!

Do it like so:
private DateTime Localdate (DateTime utc_DateTime)
{
DateTime local = new DateTime();
local = utc_DateTime.ToLocalTime();
return local;
}
But the method itself is "a bit" redundant.
Just a recommendation: Try to respect the "Pascal-case convention" embraced by C# which states that method names are capitalized and that parameter names are not (amongst other things).

use method DateTime.ToLocalTime()

Related

Set Datetime in C# based on Timezone like +08:00 [duplicate]

This question already has answers here:
Creating a DateTime in a specific Time Zone in c#
(10 answers)
Closed 1 year ago.
I have created current date time like this.
DateTime now = DateTime.UtcNow;
Now I want to convert this to different time zone. But I am getting values in timezone like +05:30, +07:00 etc..
So how can I convert this now value to that specific timezone date using this kind of timezone value.
Thanks
You need DateTimeOffset.ToOffset(TimeSpan) to convert Utc to your desired timezone.
DateTime now = DateTime.UtcNow;
DateTimeOffset dtoUtc = new DateTimeOffset(now);
TimeSpan offset = new TimeSpan(+5, 00, 00); // Specify timezone
var dtToSpecificTimezone = dtoUtc.ToOffset(offset);
Console.WriteLine(dtToSpecificTimezone.ToString());
Output:
7/11/2021 3:02:10 PM +05:00
Sample program

How to make formatException gone in Datetime [duplicate]

This question already has answers here:
Convert string to DateTime in c#
(3 answers)
DateTime.Parse throw exception in C#
(1 answer)
DateTime.Parse throwing format exception
(3 answers)
Closed 2 years ago.
Hi I just want to know how to make formatException gone when I'm inserting Datetime.
coding is in down below:
DateTime dob = Convert.ToDateTime(z);
dob.ToString("yyMMdd");
DateTime today = DateTime.Today;
today.ToString("yyMMdd");
var year = today.Year - dob.Year;
age = Convert.ToString(year);
return a;
Try use DateTime.TryParse or DateTime.TryParseExact to check your datetime string and store to your variable after parse at the same time
Use the DateTime.ParseExact() method to convert your date string z to a DateTime value -
var z = "23/09/2020";
DateTime dob = DateTime.ParseExact(z, "dd/MM/yyyy", null);
As you can see, you have to pass the date format you are trying to convert to the second parameter of the ParseExact() method as a string;

Convert String (UTC) to DateTime (EST) [duplicate]

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);
}

Is it possible to use datetime to get an object for date only in C#? [duplicate]

This question already has answers here:
A type for Date only in C# - why is there no Date type?
(14 answers)
Closed 7 years ago.
Given that I have a date and only want to display the date and not date and time, how would I go about this?
Here is the code I expect to do this:
var day = 4;
var month = 12;
var year = 2016;
DateTime someDate = new DateTime(year, month, day);
Console.WriteLine(someDate.Date.ToString("d"));
I purpose this question because there does not seem to be an object that simply shows the date without changing it to a string.
For example,
Using the someDate.Date gives the date with 00:00:00 as the time, so
12/4/2016, turns out to be 12/4/2016 00:00:00 from the actual object, how do I just get 12/4/2016 as an object without the 00:00:00?
If you need a culture specific date, try using ToShortDateString.
12/4/2016, turns out to be 12/4/2016 00:00:00 from the actual object,
how do I just get 12/4/2016 as an object without the 00:00:00?
I think the best you can do is as below:
DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy"));

Convert string(dd/MM/yyyy hh:mm) to datetime format [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 8 years ago.
I have string with datetime format dd/MM/yyyy hh:mm.I want to calculate duration between two dates but failed to get datetime in correct format.please help.
thanks in advance
After parsing date string create two dates.
DateTime date1 = new DateTime();
DateTime date2 = new DateTime();
date1 = DateTime.Parse("22/05/2013 09:50:00");
date2 = DateTime.Parse("22/05/2014 09:50:00");
Then use TimeSpan structure to calculate interval:
TimeSpan ts_interval = date2 - date1;
You can use the following properties:
ts_interval.TotalSeconds;
ts_interval.TotalMinutes;
ts_interval.TotalHours;
For more visit http://msdn.microsoft.com/en-us/library/system.timespan_properties%28v=vs.110%29.aspx
you can use build in method
DateTime.Parse("12/05/1999 18:25");
you can also check this post

Categories

Resources