.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to use date and/or time values from a shared place, in ASP.Net

Posted By:ASPEvil       Posted Date: July 25, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

How to can use shared date and time values in our code which will avoid writing same code several times for the same purpose.
 

In your ASP.Net website, sometimes the exact date and time values may needed to be used several times. So why not we put the getting date and time code in a common shared place where from we can use those code with a minimum reference code, this article shows that.


Create a class by clicking the website name of your web application and then right click on it, then select "Add New Item..." and then it will open a new window, select "Class" from that window and give a name to your class and click the "Add" button. Now it will show a message: "You are attempting to add a special file type(class) to an ASP.Net Web site. In general, to use this type of item in your site, you should place it in the 'App_Code' folder. Do you want to place the file in the 'App_Code' folder?"

Click the "Yes" button of this message. Now your file is saved in "App_Code" folder of your web application.


Now open that file and its code will be something like, following:-

[VB.Net code]

Imports Microsoft.VisualBasic
    Public Class Class1

    End Class
[C# code]

using Microsoft.VisualBasic;
public class Class1 {
} 

Now lets create the date and time functions. The date function is as follow:-

[VB.Net code]

Public Shared Function getDateofToday() As String
        getDateofToday = DateTime.Now.Date.ToString("dd-MM-yyyy").ToString
    End Function
[C# code]

public static string getDateofToday() {
        return DateTime.Now.Date.ToString("dd-MM-yyyy").ToString;
    } 

Now lets create the time function as follow:-

[VB.Net code]
Public Shared Function getTimeofNow() As String
        getTimeofNow = Now.ToString("hh:mm:ss tt")
    End Function
[C# code]
public static string getTimeofNow() {
        return Now.ToString("hh:mm:ss tt");
    } 

With the date function[Function getDateofToday()], the date format of the 2nd January 2015, will be as: 02-01-2015. You can change this format as you want. For example: if you want this format to be as: 02/01/2015, then you can use: dd/MM/yyyy with the particular function.

Note: for displaying the date value on the webpage it is no matter how you format the date value, but whenever you are inserting date values to a database, you should format the date value as that particular database accepts the date values, else you will get run time errors.



With the time function[Function getTimeofNow())], the format of the time will be as: 10:15:20 AM where it 10 is the hour(morning 10), 15 is the minute and 20 is the second(s). And the 'tt' is used to display the AM/PM with the time. You can also format the time value as you want.


Note: for displaying the time value on the webpage it is no matter how you format the time value, but whenever you are inserting time values to a database, you should format the time value as that particular database accepts the time values, else you will get run time errors.


So the total code becomes:-

[VB.Net code]

Imports Microsoft.VisualBasic
Public Class Class2
    Public Shared Function getDateofToday() As String
        getDateofToday = DateTime.Now.Date.ToString("dd-MM-yyyy").ToString        
    End Function

    Public Shared Function getTimeofNow() As String
        getTimeofNow = Now.ToString("hh:mm:ss tt")
    End Function
End Class
[C# code]

using Microsoft.VisualBasic;
public class Class2 {
    
    public static string getDateofToday() {
        return DateTime.Now.Date.ToString("dd-MM-yyyy").ToString;
    }
    
    public static string getTimeofNow() {
        return Now.ToString("hh:mm:ss tt");
    }
} 

Now we need call these functions to use the values of these functions to our code, as follow:-

[VB.Net code]

Response.Write(Class2.getDateofToday)
        Response.Write("<br/>")
        Response.Write("<br/>")
        Response.Write(Class2.getTimeofNow)
[C# code]

Response.Write(Class2.getDateofToday);
Response.Write("<br/>");
Response.Write("<br/>");
Response.Write(Class2.getTimeofNow); 


With the above code I called the date function and the time function and wrote the values of those functions to the webpage using the "Response.Write" and put two line breaks by writing: Response.Write("<br/>")

Response.Write("<br/>")


Or with C#: Response.Write("<br/>");
Response.Write("<br/>");



You can use these values on anywhere on your webpage for your particular purpose.




Thanks.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend