C
C#3y ago
Squirtle

help datetime

i want to make a program were i type in my social security number and it says which day i was born in example user writes 2000, 04, 28, computer says you are born on a Friday
44 Replies
Chiyoko_S
Chiyoko_S3y ago
so uh, since I'm not in the US, just to make it clear, the social security number is ID in format of NNN-NN-NNNN and doesn't actually contain any information related to the date of birth, right?
Squirtle
SquirtleOP3y ago
ups i mean the number you are born in so
Chiyoko_S
Chiyoko_S3y ago
so you meant date of birth
Squirtle
SquirtleOP3y ago
YYYY - MM - DD
Chiyoko_S
Chiyoko_S3y ago
right
Squirtle
SquirtleOP3y ago
yeah m b hahahaha static void inmatning() { var birthDate = new DateTime(2000, 04, 28); var thisYear = new DateTime(DateTime.Today.Year, birthDate.Month, birthDate.Day); var dayOfWeek = thisYear.DayOfWeek; Console.WriteLine(dayOfWeek); } made this so far
Chiyoko_S
Chiyoko_S3y ago
so there's a built-in type called DateTime you can use
Squirtle
SquirtleOP3y ago
but it says thursday
Chiyoko_S
Chiyoko_S3y ago
oh yeah you figured it out it seems hmmmmm
Squirtle
SquirtleOP3y ago
but it was a friday when i googled it
Chiyoko_S
Chiyoko_S3y ago
so what you want is you want to know which day of week it was on 2000/04/28?
Squirtle
SquirtleOP3y ago
yeah and later i want to translate that into what the user inputs
Chiyoko_S
Chiyoko_S3y ago
birthDate.DayOfWeek is the correct one then, I'm not sure what you're doing with thisYear thing exactly because from what I see thisYear is 2022-04-28
Squirtle
SquirtleOP3y ago
yeah i can basically remove that OH
Chiyoko_S
Chiyoko_S3y ago
so dayOfWeek which is thisYear.DayOfWeek would give the day of week your birthday would be this year
Squirtle
SquirtleOP3y ago
I FIGURED IT OUT thxxxx
Chiyoko_S
Chiyoko_S3y ago
good
Squirtle
SquirtleOP3y ago
hahahaha now i just need to make it so the userinputs it with - is that possible
Chiyoko_S
Chiyoko_S3y ago
you have two ways of going about it I see one is to read three integers, separately
Squirtle
SquirtleOP3y ago
can i make it all in one row ? and is it possible to use arrays here ?
Chiyoko_S
Chiyoko_S3y ago
basically ask "year of birth" "month of birth" "day of birth" then create a DateTime from it alternatively, take in a string in some fixed format, like yyyy-MM-dd (where yyyy, MM, dd are the actual numbers)
Squirtle
SquirtleOP3y ago
yeah
Chiyoko_S
Chiyoko_S3y ago
and use DateTime.Parse (or ParseExact) to create a DateTime object directly from the string itself it'll do the parsing for you however it has to be in that exact format anything other than that, like a random whitespace or comma in random places, will result in error not sure where you could use an array there I mean, sure, you could if you try like taking in a string like 2022-03-04, calling Split to make it a string array but why so if you could just use DateTime.Parse/ParseExact
Squirtle
SquirtleOP3y ago
how will the code look like
Chiyoko_S
Chiyoko_S3y ago
Console.WriteLine("Enter your date of birth in year - month - date order, separated by a single whitespace");
string input = Console.ReadLine();
string[] numbers = input.Split();
Console.WriteLine("Enter your date of birth in year - month - date order, separated by a single whitespace");
string input = Console.ReadLine();
string[] numbers = input.Split();
now you have three strings (which should be numbers) in the numbers array which you need to now turn into integer (I hope you know how to do that) and feed it into DateTime's constructor alternatively, using ParseExact:
Console.WriteLine("Enter your date of birth in yyyy-MM-dd format");
string input = Console.ReadLine();
DateTime date = DateTime.ParseExact(input, "yyyy-MM-dd");
Console.WriteLine("Enter your date of birth in yyyy-MM-dd format");
string input = Console.ReadLine();
DateTime date = DateTime.ParseExact(input, "yyyy-MM-dd");
ero
ero3y ago
though use TryParseExact
Chiyoko_S
Chiyoko_S3y ago
yeah, that is also a valid suggestion ParseExact will just fail with an error if it receives an invalid input TryParseExact will tell you whether it failed or not instead, giving you opportunity to retry
Squirtle
SquirtleOP3y ago
dont know how to do that :S just tried a bit
Chiyoko_S
Chiyoko_S3y ago
hint: int.Parse
Squirtle
SquirtleOP3y ago
should i create new ints 3 new ints ? int.Parse(input); this ?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
Console.WriteLine("Enter your date of birth in year - month - date order, separated by a single whitespace"); string input = Console.ReadLine(); string[] numbers = input.Split(); int.Parse(input); var birthDate = new DateTime(input[0], input[1], input[2]); var dayOfWeek = birthDate.DayOfWeek; Console.WriteLine(dayOfWeek); tried this and didnt work lmao
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
so int s = int.Parse(input); and should input be in the "()" dont know :S
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
"input" may be null here
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
the numbers[]
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
int s = int.Parse(numbers[]); tried this but it turns red
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
hmm cant seem to figure it out :/
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Squirtle
SquirtleOP3y ago
I FIGURED IT OUT THANKS hahahahahahha
Want results from more Discord servers?
Add your server