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

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

Related

C# Converting string to DateTime without using DateTimeOffset

I'm trying to bring over facebook events to my website. They will be coming from different regions. For example, an event is in Eastern Time (-4 hours difference from UTC) and my local time is Central time (-5 hours difference from UTC).
I am calling their graph API from a console app. I get the date of the events like this:
// get event items
me = fbClient.Get(url);
var startTime = me["start_time"];
var endTime = me["end_time"];
the start time shows: "2017-04-30T13:00:00-0400" object {string}
When I try to convert that string into a DateColumn type, it changes the output time to:
var dateTime = Convert.ToDateTime(startTime);
{4/30/2017 12:00:00 PM}
It shifted the hour from 13 -> 12, how do I convert the string into date using DateTime and not using DateTimeoffset?
This examples shows how to do it using DateTimeOffset, but I need mine in DateTime type?
https://stackoverflow.com/a/19403747/1019042
You can use the DateTime property of DateTimeOffset like the answer accepted in the link you've provided.
Or, if you really like to do it just with DateTime you can if you cut the timezone from the string:
var dt = DateTime.ParseExact(startTime.Substring(0,19),
"yyyy-MM-ddTHH:mm:ss",
CultureInfo.InvariantCulture);

Add hours/minute to a datetime variable in C# [duplicate]

This question already has answers here:
Add hours or minutes to the current time
(4 answers)
Closed 5 years ago.
I want to add 30 minutes to my date time variable.
My code:
string time = ViewState["CloseTime"].ToString();
DateTime Closetime = DateTime.ParseExact(time, "HH:mm:ss", CultureInfo.InvariantCulture);
Here my datetime variable is Closetime. I want to add 30 minute to it. How is it possible?
Use:
DateTime currentTime = DateTime.Now;
DateTime x30MinsLater = currentTime.AddMinutes(30);
Console.WriteLine(string.Format("{0} {1}", currentTime, x30MinsLater));
Result:
4/11/2017 3:53:20 PM 4/11/2017 4:23:20 PM
Try AddMinutes(),
DateTime newDate = Closetime.AddMinutes(30);
Simply use CloseTime.AddMinutes(30);. Make sure that this results in a new DateTime object.
var newTime = CloseTime.AddMinutes(30);
To add 30 minutes to a DateTime variable, the following will work:
CloseTime = CloseTime.AddMinutes(30);
There are similar methods for adding seconds, hours, days, etc.
See here for the documentation: Methods for DateTime Struct

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

How do I get the system date and time and store in a variable [duplicate]

This question already has answers here:
How to get the current date without the time?
(15 answers)
Closed 9 years ago.
I want to store the current date and time in a variable using C#. How would I do that if it has to be in the format YYYY, MM, DD, HH, MM, SS? I.e. I know I can store midnight on January 1st, 2013 as;
var time= new DateTime(2013, 1, 1, 0, 0, 0);
How do I do this with the system date and time?
var time = DateTime.Now;
Format the time when you retrieve it, not when you store it - i.e. -
string formattedTime = time.ToString("yyyy, MM, dd, hh, mm, ss");
You need DateTime's static field Now:
var time = DateTime.Now;
You can try using the DateTime.Now like this:-
var time = DateTime.Now;
Here it is:
var time = DateTime.Now;
DateTime currentTime = DateTime.Now;

Categories

Resources