C
C#5mo ago
noobmaster69

✅ 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
No description
36 Replies
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
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.
noobmaster69
noobmaster69OP5mo ago
:harold:
leowest
leowest5mo ago
Console.Write("Write your age");
var age = Console.ReadLine();
Console.Write("Write your age");
var age = Console.ReadLine();
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
noobmaster69
noobmaster69OP5mo ago
I want to input in the same line like 10-20-30 not 10 -20 -30
noobmaster69
noobmaster69OP5mo ago
No description
leowest
leowest5mo ago
10-20-30 would be the 11-07-24 in your image above as an example?
noobmaster69
noobmaster69OP5mo ago
Yessir
leowest
leowest5mo ago
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
noobmaster69
noobmaster69OP5mo ago
yes just that much I’ll check that real quick
leowest
leowest5mo ago
see the link above
noobmaster69
noobmaster69OP5mo ago
I can’t do it
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
Console.Write("Write your age");
var age = Console.ReadLine();
var datetime = DateTime.Parse(age);
Console.Write("Write your age");
var age = Console.ReadLine();
var datetime = DateTime.Parse(age);
noobmaster69
noobmaster69OP5mo ago
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
leowest
leowest5mo ago
what have u tried what did not work show some code so we can work out where ur not doing well
noobmaster69
noobmaster69OP5mo ago
I just don’t get it okay Let me try the code from sine
leowest
leowest5mo ago
just show how u were doing it before so we can look and explain to you
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
Keep in mind it won't check for null/empty or invalid strings
leowest
leowest5mo ago
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
noobmaster69
noobmaster69OP5mo ago
No description
noobmaster69
noobmaster69OP5mo ago
im trying it make it output 10/10/24 with no hour and hopefully that string datainput will work the same when user input
leowest
leowest5mo ago
well you just have to apply format or use .Date
leowest
leowest5mo ago
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")
Angius
Angius5mo ago
Seems like
Console.Write("Enter date: ");
var date = DateTime.ParseExact(Console.ReadLine(), "dd-mm-yy")
Console.Write("Enter date: ");
var date = DateTime.ParseExact(Console.ReadLine(), "dd-mm-yy")
no?
noobmaster69
noobmaster69OP5mo ago
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
noobmaster69
noobmaster69OP5mo ago
No description
No description
noobmaster69
noobmaster69OP5mo ago
how to make user be able to in put the date
noobmaster69
noobmaster69OP5mo ago
i got it
noobmaster69
noobmaster69OP5mo ago
=
No description
noobmaster69
noobmaster69OP5mo ago
now then how do i use the month and year to calculate caz thats the whole point of entering this 😂
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
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.
Pobiega
Pobiega5mo ago
I know I'm late to the party, but if you only care about the date... use DateOnly
noobmaster69
noobmaster69OP5mo ago
I think we’re pass that just trying make input and split data work
noobmaster69
noobmaster69OP5mo ago
why the middle always 0
No description
noobmaster69
noobmaster69OP5mo ago
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
Want results from more Discord servers?
Add your server