Setting up a date time [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been given an assignment by a friend for me to practise on C#. He wants me to set up a fitness counter, where he can add how many pushups/pullups etc he did, and then in a text file, print the date on which he entered that information (already done) and the amount of workout he did (already done). However, he also told me to set up a week and month system, where i will get everyday of his workout and print it as a whole in a weekly report, and then get all the weekly reports and print them as a month. I cant wrap my head around and find a way to approach this. How could i do it? Ive thought of getting each day from the file.txt and printing it out as a sum etc, but i cant find the appropriate code. Thanks a lot!
This is to get the day :
DateTime localDate = DateTime.Now;
Console.WriteLine("The date today is " + localDate.ToShortDateString());
Console.ReadLine();
And I've tried DateTime.Now.DayOfWeek; etc but nothing

What you can do for weeks is use the weeknumber. As there are different numbering systems, that isn't a property on 'DateTime' but is retrievable from a calendar. Use
CultureInfo ciCurr = CultureInfo.CurrentCulture;
int weekNum = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
For months, you can retrieve the month property of the date. Remember that with these rules, weeks do not add up to month and year totals, as weeks van span two months or years.

Related

How to delete rows in SQLite database where rows older than 30 days using c# ? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 13 days ago.
Improve this question
There is a SQLite data table with a lastVisited column which is REAL type .
I hope to delete rows using C# where lastVisited column are older than 30 days (This is a parameter, maybe it's 45, 60, or other), how can I do?
REAL is one the supported data type for storing dates :
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November
24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Julian days would be in the 2400000..2500000 range, and you can delete old values with something like :
DELETE ...
WHERE lastVisited < julianday('now') - 30

'string' to 'System.IFormatProvider' Error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to get "ago" time within C# but am currently struggling as I get this Error.
Basically this is what I am trying.
var dateAgo = $"{DateTime.Now.Subtract(booking.CreatedAt.Date)}";
booking.createdAt.Date is where my date for my booking is saved.
I can show #dateAgo in my HTML but the problem is when I try something like
#DateTime.Now.Date.Subtract(booking.CreatedAt.Date).ToString("d")
which sadly does not work.
DateTime.Subtract() yields a TimeSpan object. ToString("d") is not a standard TimeSpan format string. See Standard TimeSpan Format Strings.
You want the custom format string "d" which outputs the number of days. For this, change "d" to "%d":
#DateTime.Now.Date.Subtract(booking.CreatedAt.Date).ToString("%d")
Or you can simply extract the Days from the returned TimeSpan object which will return the number of days:
#DateTime.Now.Date.Subtract(booking.CreatedAt.Date).Days.ToString()
Use DateTime.Now.Subtract(booking.CreatedAt.Date).Days
Subtract() produces a TimeSpan, which is incompatible with using the "d" format specifier by itself. Try something else, e.g. .ToString("%d") or .ToString("c").
Or if needed, the TimeSpan provides .Days, .Hours etc properties which can be used instead.

Best method to find difference between two DateTime objects? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to find difference between two DateTime objects. I know I can use DateTime.Subtract() and overloaded operator '-' (e.g DateTime - DateTime). But someone told me that these two techniques are not good enough, and there is a method DateDiff or something, which I should use and which is best to use. I want to know if it is true? If it is, then how the above two are inferior from any perspective?
DateDiff does not compare dateTime instances.
It compares specific parts of the DateTime instances - for example, the months, or hours.
For instance:
var lasyDayOf2018 = new DateDime(2018, 12, 31);
var firstDayOf2019 = lasyDayOf2018.AddDays(1);
var monthDiff = DateDiff(DateInterval.Month, lasyDayOf2018, firstDayOf2019)
monthDiff will be 1, even though only one day has passed between the two dates.
It's documented in the Remarks section of the DateDiff documentation page:
If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2. Similarly, the return value for DateInterval.Month is calculated purely from the year and month parts of the arguments, and for DateInterval.Quarter from the quarters containing the two dates.
For example, when comparing December 31 to January 1 of the following year, DateDiff returns 1 for DateInterval.Year, DateInterval.Quarter, or DateInterval.Month, even though at most only one day has elapsed.
To get the actual difference between different instances of DateTime, either use Subtract or -, as you wrote in the question.
Whether or not there is a problem, and the solution to that problem really depends on the situation and why you're computing the difference. There are two main problems with DateTime.Subtract() and operator -.
The first is that they both ignore the time zone portion of the data. So, for example, if you have two dates, 2019-07-18 12:00:00 UTC and 2019-07-18 06:00:00 MDT (local time), even though they represent the same moment in time (as measured from different time zones), DateTime.Subtract() will tell you that they're 6 hours apart.
The second problem is that you often want to know the answer in terms of units other than ticks, sometimes even something that cannot be converted to a fixed number of ticks. For example, if you want the difference in the months represented by two DateTimes, you cannot compute that from the difference in ticks (which is what DateTime.Subtract() is based on), because different months have different lengths. For example, for the dates '2019-06-30 23:59:59' and '2019-07-01 00:00:00' (in the same time zone) there is only a one second difference, but the months are different, so is the difference in months one or zero? It depends on what you want to use the answer for.

How to ask C# date of birth in a certain format with limitations? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm very new to C# and I think I got something on my mind but can't quite figure it out.
So I got this school project that needs to ask the first name, last name and date of birth of the user but with certain limitations. I can figure the names out but the date problem persists.
The date of birth should be in DD.MM.YYYY. format and have certain limitations:
Date should be between 01-31
Month between 01-12
Year between 1900-2050
I can get it to ask but it won't specify the format and I don't know what variable to use.
To parse dates in custom format, DateTime.TryParse method is best fit.
You just need to find culture, which uses your date format, for example "fr-CH".
Then you use mentioned method to check if format of date was correct. It automatically checks if date is vaild, i.e. month is between 1 and 12, day of month is in correct range (1 through 28,29,30 or 31 depending on month and year).
You just need to additionally check the year.
Try this code (I used short-circuiting operator &&, so if parsing was successfull, then check the year):
DateTime dt;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-CH");
DateTimeStyles styles = DateTimeStyles.None;
if(DateTime.TryParse("28.01.2018", culture, styles, out dt)
&& dt.Year >= 1900 && dt.Year <= 2050) // here you check additionally if year is in correct range
Console.WriteLine("Date successfully parsed!");

Why does DateTime.Parse() get wrong year when parsing dd-MMM date? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I created an application in which the user enter a month-year dd-MMM date as input, and this date is parsed using the DateTime.Parse() method. It is working fine on my PC.
Dim txtDate as String = combobox1.Text
Dim dtDate as DateTime = DateTime.Parse(txtDate)
Example:
DateTime.Parse("01-Dec")
Result:
2017-12-01 00:00:00
But when running this application on other machines, it returns 1899 year.
1899-12-01 00:00:00
After searching I found a similar question here: Time parsing Issue using DateTime.ParseExact() and it is marked as answered, but there is no solution for this situation
Does anyone knows what causes this problem?
Note: it is an old application I am working on. I know that it is recommended to replace it with a DateTime picker, but the issue is very confusing
Date.Parse without specifying an IFormatProvider will use the current user's Culture settings to determine what formats to parse, so 01/04/2017 will be 2017-April-01 on most computers (dd/MM/yyyy) but 2017-Jan-04 on computers in the US (MM/dd/yyyy).
If you're using the same format everywhere, you should use DateTime.ParseExact and provide an explicit format.
As for your specific problem, the string 01-Dec does not specify a year component, the computer must therefore infer the date, and how that date is inferred is often down to the Culture setting too.

Categories