DateTime.Parse changing date? [duplicate] - c#

This question already has answers here:
Why does DateTime.Now.ToString("u") not work?
(3 answers)
Closed 5 years ago.
This is my code snippet:
public bool getEffDate()
{
testfunction(DateTime.Today.ToString("u"));
return true;
}
private bool testfunction(string modDate)
{
modDate = DateTime.Parse(modDate).ToString("yyyy-MM-dd");
return true;
}
DateTime.Today.ToString("u") - Returns current date
Whereas, modDate returns current_date - 1..
Can someone help me with the UTC date function? Why is the Parse function bringing the previous day?

This code worked for me.
modDate = DateTime.Parse(modDate, null, DateTimeStyles.AdjustToUniversal).ToString("yyyy-MM-dd");
I am assuming your timezone is Universal. You need to specify the timezone as per your requirements.

Related

C# - How can I check if DateTime has time [duplicate]

This question already has answers here:
What is the best way to determine if a System.DateTime is midnight?
(3 answers)
Closed 17 days ago.
How can I validate if a give DateTime has actual time not just the default midnight time (00:00:00).
DateTime.TryParse("2022-11-01T14:52:17", out DateTime withTime1); // has time
DateTime.TryParse("2022-11-01T00:00:01", out DateTime withTime2); // has time
DateTime.TryParse("2022-11-01T00:00:00", out DateTime noTime ); // doesn't have time
None of what you posted are DateTime values. They are strings. I'll answer the question you actually asked though:
if (myDateTime.TimeOfDay == TimeSpan.Zero)
{
// Time is midnight.
}
else
{
// Time is not midnight.
}
From your example it depends if you want to verify that as a string or not
as a string you could do something like this
if(Datetime.toString().Split('T')[1] == "00:00:00")
return false; //"no time" per your explanation
else
return true; //time

Convert Int to Time like 123430 to 12:34:30 [duplicate]

This question already has answers here:
Parsing Integer Value As Datetime
(5 answers)
Closed 6 years ago.
I would like to Convert Int to Time like 123430 to 12:34:30 in c#. Below code didnt work
DateTime dateTime = DateTime.ParseExact(timeint.ToString(), "HH:mm:ss",
CultureInfo.InvariantCulture);
The format string is the format that the inputted string is in. For example in your case HHmmss, just remove the colons and you should be fine.
Like:
DateTime dateTime = DateTime.ParseExact(timeint.ToString(), "HHmmss",
CultureInfo.InvariantCulture);

while loop returning same date value even after adding a day each time - c# [duplicate]

This question already has answers here:
DateTime.AddDays() not working as expected
(3 answers)
Closed 7 years ago.
Am I doing something very silly here, fromDate value always remains what has been passed.
public List<String> GetDates(DateTime fromDate, DateTime toDate)
{
List<String> Dates = new List<String>();
while (fromDate <= toDate)
{
Dates.Add(fromDate.ToShortDateString());
fromDate.AddDays(1);
}
return Dates;
}
I can't figure out why, Please advise.
you need to assign to fromDate, AddDays() does not modify the instance on which it is called:
fromDate = fromDate.AddDays(1);
If you look at the method description of .AddDays, you'll see that it actually returns a datetime object, it does not actually modify the variable in which AddDays is being called.
You can get your desired behavior by:
fromDate = fromDate.AddDays(1);
See more info at:
https://msdn.microsoft.com/en-us/library/system.datetime.adddays%28v=vs.110%29.aspx
The MSDN Help for AddDays has the following sentence worth noting, towards the end of the page under "Remarks":
This method does not change the value of this DateTime. Instead, it
returns a new DateTime whose value is the result of this operation.
Simply store the returned DateTime object and you are good to go.
fromDate = fromDate.AddDays(1);

DateTime difference from current date to figure out a past date [duplicate]

This question already has answers here:
How to subtract one year from DateTime [closed]
(4 answers)
Closed 8 years ago.
I am trying to find a date (string) that is 5 years from current date in the past:-
DateTime.UtcNow.Date - DateString = 5 years
Can anyone help me formulate this into c# syntax?
var myDate = DateTime.UtcNow;
var newDate = myDate.AddYears(-5);
You can add or subtract a TimeSpan from a DateTime object to get another DateTime object.
var fiveYearsAgo = DateTime.Now - TimeSpan.FromYears(5);
var fiveYearsFromNow = DateTime.Now + TimeSpan.FromYears(5);

c# previous week start date [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate previous week's start and end date
I need to get the previous week start date in c#
for eg. today is Jan 9th.
previous week start date is Jan 1th.
I am using the following code
DayOfWeek weekStart = DayOfWeek.Sunday;
DateTime startingDate = DateTime.Today;
while (startingDate.DayOfWeek != weekStart)
startingDate = startingDate.AddDays(-1);
DateTime previousWeekStart = startingDate.AddDays(-7);
Is this the best way in c#
Thanks
As previously stated the duplicate's response will probably do for you
mondayOfLastWeek = DateTime.Now.AddDays( -(int)DateTime.Now.DayOfWeek - 6 );
sundayOfLastWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek)
Try:
DateTime.Today.AddDays(-7);

Categories

Resources