Click here to Skip to main content
15,034,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create a website to calculate the date from a specific time to today's date. I tried more than one code and it did not work for me pleas help 


What I have tried:

Dim date2Entered As String = InputBox("Enter a date")

Try
    Dim date2 As Date = Date.Parse(date2Entered)
    Dim date1 As Date = Now

    ' Determine the number of days between the two dates.
    Dim days As Long = DateDiff(DateInterval.Day, date1, date2)

    ' This statement has a string interval argument, and
    ' is equivalent to the above statement.
    'Dim days As Long = DateDiff("d", date1, date2)

    MessageBox.Show("Days from today: " & days.ToString)
Catch ex As Exception
    MessageBox.Show("Invalid Date: " & ex.Message)
End Try
Posted
Updated 7-Jun-21 19:44pm
Comments
Richard MacCutchan 7-Jun-21 7:34am
   
"it did not work"
We have no idea what that means. Please use the Improve question link below your question, and add complete details.
Richard Deeming 7-Jun-21 11:42am
   
You've tagged this as ASP.NET, and said you want to create a website.

Neither InputBox nor MessageBox.Show will work in a website, because your code is running on the server.

They might appear to work when you're debugging your code in Visual Studio. But that's only because, in that specific case, the client and the server are the same computer.

As soon as you deploy your code to a real server, one of two things will happen. In the best case scenario, your code will throw an exception telling you that you cannot display a user interface from a non-interactive application. If that doesn't happen, then your code will display the UI on the server where nobody will ever see it, and then hang waiting for an administrator to log into the server and acknowledge the thousands of messages you've displayed.

1 solution

Hi Meer25

We share the same destiny. I saw your profile and noticed your reputation is "under water". The same as mine on 'Stackoverflow' ;-)

Let me start with a consideration: over the years I noticed Microsoft did its best to confusing developers.
For example. when you are doing a web project and you double click over a button, to manage the 'click' event, the IDE writes for you the method for the event handler. That's good. What is wrong is that the method is server-side (run at server)
Why that is wrong?

The first big insight a new developer should have is that a web application is something that 'lives' in two different worlds with very different roles!
1) the so-called 'backend': the code that runs on a server, a machine that is waiting for an HTTP request and depending on the request returns an HTTP response (normally an HTML string: the web page)
2) The so-called 'frontend': the web page. Something that 'lives' into a browser waiting for an action from the user.

The frontend can be a project by itself: HTML pages without any server-side code (all my tips&tricks except logger for log4Net are front-end projects) In that case the only responsibility the server has is to provide all the files the page requests without processing anything.

Reading your question is absolutely clear you didn't have that insight!

1) your question should be managed client-side (another name for the frontend) using Javascript. (don't use any framework. start using javascript. And only when you will become confident with javascript start using the framework you prefer)

Please copy and paste this code and save it as "index.htm".
But: for this time, do not use any IDE like VSCode or Visual Studio. Just use any text editor like Notepad or Notepad++ or what else.
(The browser could be a perfect IDE, but now it is too early)

<!DOCTYPE html>
<html>
<title>Datediff</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
	<script>
		function button_onclick(btn){
			try{
				// keep the page in debug-mode, this will hel to understand what happens. (hit F12 on the browser)
				// click F11 to debug (in the browser) step by step this code;
				debugger;
				
				var now = new Date();
			
				var input = window.prompt("Please enter a valid date in the format of 'dd-mm-yyyy'.", "01/01/2000");
				var ticks = Date.parse(input);
				var date = new Date(ticks);
				var diff = daysDiff(date, now);
				
				alert(diff);
			}
			catch(jse){
				alert(jse.message);			
			}
		}
		
		
		
		function daysDiff(d1, d2) {
			var t2 = d2.getTime();
			var t1 = d1.getTime();
			
			// there are: 
			// 1.000 ticks in a second
			// 3.600 seconds in an hour
			// 24 hours in a day.
			return parseInt((t2-t1)/(24*3600*1000));
		}

	</script>
</head>
<body>
	<button onclick="button_onclick(this)">Date diff</button>      
</body>
</html>


It should run and it's a perfect "translation" of your VB.NET code.
Since you used the messageBox and InputBox it's clear your intention to interact with the user.
On a web page, you should manage the interaction with the user, client-side as much as you can.
In the case, you proposed you can!
Ask the server (the backend) only if you are forced (for example the user asked for data stored in a DB)

I hope this helps

cheers

Vincenzo
   
Comments
Meer25 8-Jun-21 3:02am
   
thank you very much
but I need to work in visual studio 2019
CodeErgoSum 8-Jun-21 7:40am
   
Sorry Meer25 seems to me you don't get the point

If you want to develop with visual studio OK no problem you can include the example in your project.
That is not the point.
The fact is that your question shows you do not have a correct idea of how a web application works.
As 'Richard Deeming' pointed out you can't interact with the user server-side (InputBox nor MessageBox)
That's the point.

The example I provided is intended to
- let you know how to interact properly with the user using javascript
- show you, that some things should be done client-side. (other server-side)

that's it.

cheers
Meer25 9-Jun-21 1:26am
   
thank you I understand now .
My English language not good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900