✅ how do i make user input date in the same line
Like Date/Month/Year
Example 30/12/24
And use those number for calculate later
Currently each time I wanna enter data it always break into a newline
36 Replies
https://learn.microsoft.com/en-us/dotnet/standard/base-types/parsing-datetime
Or create your own parser.
Convert strings to DateTime - .NET
Learn techniques to parse strings that represent dates and times to create a DateTime from the date and time string.
:harold:
This will make the user write in the same line of Write your age
once he finishes it will go to the next line
beyond that u would have to mess with the console.cursorposition or something
ReadLine expect u to send [enter] to take the input which is essentially a new line
and
Console.WriteLine will write whatever text u put into it and add a new line after it whereas Write does not
but without seeing some code and your intent its hard to know if that is all u need
I want to input in the same line like 10-20-30 not
10
-20
-30
10-20-30 would be the 11-07-24 in your image above as an example?
Yessir
so u dont want the user to type the whole date u have 3 console.readline?
because you can type in 1 Console.ReadLine() the full date and then just use DateTime.TryParse
yes just that much
I’ll check that real quick
see the link above
I can’t do it
I just want to be able to enter those 11 7 24 value in the same line and later Beble use those date to calculate smthing
what have u tried what did not work
show some code so we can work out where ur not doing well
I just don’t get it
okay
Let me try the code from sine
just show how u were doing it before
so we can look and explain to you
Keep in mind it won't check for null/empty or invalid strings
blindly trying code u dont understand won't help u right now
so seeing what ur not understanding in your code helps us to explain
im trying it make it output 10/10/24
with no hour
and hopefully that string datainput will work the same when user input
well you just have to apply format
or use .Date
Then just print the date field...
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.date?view=net-8.0#system-datetime-date
DateTime.Date Property (System)
Gets the date component of this instance.
either should work, u can read more on the link above and on https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
parsedData.Date
parsedData.ToString("dd/MM/yyyy")
Seems like
no?
It’s said “ParseExact” takes 2 arguments
Severity Code Description Project File Line Suppression State
Error (active) CS1501 No overload for method 'ParseExact' takes 2 arguments Chap3_InterestCal D:\SETEC\SETEC C#\Chap3_InterestCal\Program.cs 22
Yea that does it but rn I need to figure out how to make user be able to enter the date in a single line
how to make user be able to in put the date
:maxpain:
i got it
=
now then how do i use the month and year to calculate caz thats the whole point of entering this 😂
Look at the properties in the DateTime structure. You can get each individual time format down to the millisecond
https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=net-8.0
DateTime Struct (System)
Represents an instant in time, typically expressed as a date and time of day.
I know I'm late to the party, but if you only care about the date... use
DateOnly
I think we’re pass that
just trying make input and split data work
why the middle always 0
nvm all of that im going this route
Console.Write("Enter Date numbers separated by - : ");
string input = Console.ReadLine();
string[] inputs = input.Split('-');
if (int.TryParse(inputs[0], out int number1) && int.TryParse(inputs[1], out int number2) && int.TryParse(inputs[2], out int number3)) { Console.WriteLine($"Number 1: {number1}"); Console.WriteLine($"Number 2: {number2}"); Console.WriteLine($"Number 3: {number3}"); // You can use number1 and number2 variables for further processing } else { Console.WriteLine("Invalid input. Please enter two integers separated by a space."); } /close
if (int.TryParse(inputs[0], out int number1) && int.TryParse(inputs[1], out int number2) && int.TryParse(inputs[2], out int number3)) { Console.WriteLine($"Number 1: {number1}"); Console.WriteLine($"Number 2: {number2}"); Console.WriteLine($"Number 3: {number3}"); // You can use number1 and number2 variables for further processing } else { Console.WriteLine("Invalid input. Please enter two integers separated by a space."); } /close