Time operations using session & net in the ashx File

Source: Internet
Author: User

Use session in the ashx file:

If you want. use session in ashx (general processing program) to directly use context without changing some information. session ["uid"] will show that "object reference is not set to object instance", that is, the value is null. The solution is:

1. Import in The namespace: using system. Web. sessionstate;

2. Let it inherit the irequiressessionstate interface:

Public class handler1: ihttphandler, irequiressessionstate

{

Public void processrequest (httpcontext context)

{

In this way, you can directly use context. session ["uid;

}

}

Note:

1. If you want to read the session content in httphandler (that is, when I assign a value to context on a page that inherits irequiressessionstate. session ["uid"] = "kimhillzhang", so I have to inherit from ireadonlysessionstate on the page where I want to get this session value. session ["uid"]. tostring (), otherwise the value is null), The ireadonlysessionstate interface must be implemented in the ihttphandler class.
2. If you want to read and write session content in httphandler, You need to implement irequiressessionstate at the same time in the class implementing ihttphandler.

 

 

. Net operations on time:

Datetime date = datetime. Today; // 'today' gets the current date
Response. Write (date );
// Display 0:00:00
Datetime dt = datetime. Now; // 'now 'obtains the current time, including year, month, day, hour, minute, and second.
Response. Write (Dt. tostring ());
// Display 20:36:12
Response. Write (Dt. Date. tostring (); // 'date' gets the date part of the current instance dt
// Display 0:00:00
Response. Write (Dt. Day. tostring (); // 'day' obtain the date indicated by the current instance dT as the day of the month.
// Display 13
Response. Write (Dt. dayofweek. tostring (); // 'dayofweek 'obtains the day of the week represented by the current instance dt
// Display Wednesday
Response. Write (Dt. dayofyear. tostring (); // 'dayofyear' obtain the date represented by the current instance dT as the day of the year
// Display 13
Response. Write (Dt. Hour. tostring (); // 'hour' gets the hour of the date represented by the current instance dt
// Display 20
Response. Write (Dt. millisecond. tostring (); // 'milliscond' gets the millisecond part of the date represented by the current instance dt
// Display 687
Response. Write (Dt. Minute. tostring (); // obtain the minute of the date represented by the current instance dt
// Display 36
Response. Write (Dt. Month. tostring (); // 'month' gets the month of the date represented by the current instance dt
// Display 1
Response. Write (Dt. Second. tostring (); // 'second' gets the second of the date represented by the current instance dt
// Display 12
Response. Write (Dt. timeofday. tostring (); // 'timeofday' obtains the time part of the date represented by the current instance DT.
// Displays 20:36:12. 6875000
Response. Write (Dt. year. tostring (); // 'Year' gets the year of the date represented by the current instance dt
// Display 2010

Datetime related methods:

1. Add method: add the specified timespan value to the Instance value.

Public datetime add (
Timespan Value
) The timespan parameter contains the time interval to be added. The object represents the time interval or duration, measured by plus or minus days, hours, minutes, seconds, and decimals of seconds. For example, datetime dt = datetime. now; timespan Ts = new timespan (1, 1, 1, 1); // The parameter indicates 1 day, 1 hour, 1 minute, 1 second, 1 millisecond response. write (DT. add (TS ). tostring (); 2. adddays method: add the specified number of days to the Instance value public datetime adddays (
Double Value
)
 
For example, datetime dt = datetime. Now; response. Write (Dt. adddays (30). tostring (); // Add the excess parts to the previous Unit in order.
 
3. addhours: add the specified hour to the Instance value
 
4. addmilliseconds: add the specified number of milliseconds to the Instance value.
 
5. addminute: Same as above
 
6. addmonths: Same as above
 
7. addseconds: Same as above
 
8. addyears: Same as above
 
9. Compare: Compares two instances of datetime and returns the indication of their relative values.
Public static int compare (
Datetime T1,
Datetime T2
)
Example: datetime dt1 = new datetime (100); datetime dt2 = new datetime (200); If (datetime. compare (dt1, dt2)> 0) response. write ("dt1> dt2"); If (datetime. compare (dt1, dt2) = 0) response. write ("dt1 = dt2"); If (datetime. compare (dt1, dt2) <0) response. write ("dt1 <dt2 ");
 
10. compareto: Compare the instance with the specified object and return an indication of the relative value (INT) of the two.
For example, datetime dt1 = new datetime (100); datetime dt2 = new datetime (200); int I = dt1.compareto (dt2); response. Write (I );
11. daysinmonth: returns the number of days in the specified year and month.
For example: int I = datetime. daysinmonth (2010, 1); response. Write (I );
12. Equals method: returns a value indicating whether the instance is equal to the specified datetime instance. Return bool (true or false)
For example, datetime dt1 = datetime. Now. adddays (1); datetime dt2 = datetime. Now; response. Write (dt1.equals (dt2 ));
13. tolongdatestring converts the value of this instance to its equivalent long date string representation.
14. tolongtimestring converts the value of this instance to its equivalent long-time string representation.
15. tow.datestring converts the value of this instance to its equivalent short date string representation.
16. tow.timestring converts the value of this instance to its equivalent short-time string representation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.