Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> Asp
 
Date Manipulation In ASP
 
Extracting parts of the date out with DatePart()

DatePart(PartOfDate, Date[, FirstDayOfWeek, FirstDayOfYear])
- returns the request part of the date
- optional parameters are setting the first day of the week (defaults to Sunday), and setting the first week of the year (defaults to Jan 1).

FirstDayOfWeek optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Sunday (the default value if this parameter is not supplied)
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thursday
6 - Friday
7 - Saturday

FirstDayOfYear optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Start with the week that has Jan 1 in it (the default value if this parameter is not supplied)
2 - Start with the first week that contains at least four days in the new year.
3 - Start with the first week that is a full week in the new year.

- I recommend just leaving the two optional parameters out unless they specifically apply to what you are currently trying to do.

PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second

An example of what each of those values will output is:

var1 = Now()
Response.Write("var1 = " & var1 & "<br>")
Response.Write("var1 part yyyy = " & DatePart("yyyy", var1) & "<br>")
Response.Write("var1 part m = " & DatePart("m", var1) & "<br>")
Response.Write("var1 part q = " & DatePart("q", var1) & "<br>")
Response.Write("var1 part y = " & DatePart("y", var1) & "<br>")
Response.Write("var1 part d = " & DatePart("d", var1) & "<br>")
Response.Write("var1 part w = " & DatePart("w", var1) & "<br>")
Response.Write("var1 part ww = " & DatePart("ww", var1) & "<br>")
Response.Write("var1 part h = " & DatePart("h", var1) & "<br>")
Response.Write("var1 part n = " & DatePart("n", var1) & "<br>")
Response.Write("var1 part s = " & DatePart("s", var1) & "<br>")

Results:
var1 = 9/1/99 1:18:15 PM
var1 part yyyy = 1999
var1 part m = 9
var1 part q = 3
var1 part y = 244
var1 part d = 1
var1 part w = 4
var1 part ww = 36
var1 part h = 13
var1 part n = 18
var1 part s = 15
Adding & Subtracting Dates & Times

VBScript provides developers with a wonderful function called DateAdd(). With this function you can add or subtract from a date or time.

DateAdd(PartOfDate, AmountToChange, Date)
- returns a date that has been changed the specified amount
- AmountToChange is the number to which you want the date to change (1, -4, 10, etc).
- Date is a valid date
- PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second

An example of what each of those values will output is:

var1 = Now()
Response.Write("var1 = " & var1 & "<br>")
Response.Write("var1 plus 10 days = " & DateAdd("d", 10, var1) & "<br>")
Response.Write("var1 minus 10 days = " & DateAdd("d", -10, var1) & "<br>")
Response.Write("var1 plus 3 months = " & DateAdd("m", 3, var1) & "<br>")
Response.Write("var1 minus 3 months = " & DateAdd("m", -3, var1) & "<br>")
Response.Write("var1 plus 2 hours = " & DateAdd("h", 2, var1) & "<br>")
Response.Write("var1 minus 2 hours = " & DateAdd("h", -2, var1) & "<br>")

Results:
var1 = 9/1/99 1:41:04 PM
var1 plus 10 days = 9/11/99 1:41:04 PM
var1 minus 10 days = 8/22/99 1:41:04 PM
var1 plus 3 months = 12/1/99 1:41:04 PM
var1 minus 3 months = 6/1/99 1:41:04 PM
var1 plus 2 hours = 9/1/99 3:41:04 PM
var1 minus 2 hours = 9/1/99 11:41:04 AM
Find the amount of time between dates with DateDiff()

Find out how much time occurs between two dates is easy with DateDiff(). This function is useful if you need to find the amount of time between one date and another, or if you need to find out how much time is left until the end of the year.

DateDiff(PartOfDate, Date1, Date2[, FirstDayOfWeek, FirstDayOfYear])
- returns the amount of time in between the two dates in the form of PartOfDate, therefore if you set PartOfDate equal to year, the function will tell you how many years are in between Date1 and Date2 even if you are comparing Dec 31 and Jan 1. If you set PartOfDate equal to day then the function will return to you the number of days between Date1 and Date2.
- if Date1 occurs after Date2 the function will return to you a negative number.
- optional parameters are setting the first day of the week (defaults to Sunday), and setting the first week of the year (defaults to Jan 1).

FirstDayOfWeek optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Sunday (the default value if this parameter is not supplied)
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thursday
6 - Friday
7 - Saturday

FirstDayOfYear optional parameter can have one of the following values:
0 - Use National Language Support API setting
1 - Start with the week that has Jan 1 in it (the default value if this parameter is not supplied)
2 - Start with the first week that contains at least four days in the new year.
3 - Start with the first week that is a full week in the new year.

- I recommend just leaving the two optional parameters out unless they specifically apply to what you are currently trying to do.

PartOfDate parameter can have the following values:
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second

Example of use:

var1 = "04/04/1999"<br> var2 = "01/11/2000"<br>

Response.Write("var1 = " & var1 & "<br>")
Response.Write("var2 = " & var2 & "<br>")
Response.Write("var1 to var2 is " & DateDiff("d", var1, var2) & " days <br>")
Response.Write("var1 to var2 is " & DateDiff("m", var1, var2) & " months <br>")
Response.Write("var1 to var2 is " & DateDiff("yyyy", var1, var2) & " year(s) <br>")

Results:
var1 = 04/04/1999
var2 = 01/11/2000
var1 to var2 is 282 days
var1 to var2 is 9 months
var1 to var2 is 1 year(s)
Getting the day, month, year, weekday, and weekday name

There are five functions that you can call, pass a valid date, and they will return to you the requested part of the date. Below I will detail each of them and show an example of their use:

Day(ValidDate)
- returns the day of the ValidDate, a number between 1 and 31

Month(ValidDate)
- returns the month of the ValidDate, a number between 1 and 12

Year(ValidDate)
- returns the year of the ValidDate, a number like 1997

Weekday(ValidDate[, FirstDayOfWeek])
- returns the weekday of the ValidDate, a number 1 - 7
- what the function returns depends on what day is the first day of the week, you can set this, or use the default - Sunday
- to set the FirstDayOfWeek parameter use 1-7, where 1 is Sunday

WeekDayName(DayOfWeek, Abbreviate, FirstDayOfWeek)
- returns the name of the day of the week that you specify.
- DayOfWeek is a number representing the day of the week you want the name for (1-7)
- Abbreviate is a boolean value to tell the function whether or not you want the name that comes back to be abbreviated or not (true or false)
- to set the FirstDayOfWeek parameter use 1-7, where 1 is Sunday

An example of the above functions in use is:

var1 = "04/04/1999"<br>
Response.Write("var1 = " & var1 & "<br>")
Response.Write("For var1 the day is " & Day(var1) & "<br>")
Response.Write("For var1 the month is " & Month(var1) & "<br>")
Response.Write("For var1 the year is " & Year(var1) & "<br>")
Response.Write("For var1 the weekday is " & WeekDay(var1, 1) & "<br>")
Response.Write("For var1 the weekdayname is " & WeekDayName(WeekDay(var1), false, 1) & "<br>")

Results:
var1 = 04/04/1999
For var1 the day is 4
For var1 the month is 4
For var1 the year is 1999
For var1 the weekday is 1
For var1 the weekdayname is Sunday
 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› Display records from Microsoft Excel in ASP

› Cache Data within your Application - Asp.Net

› Your New Website Is Inside

› The Low Down On Cascading Style Sheets

› Design An Online Chat Room With PHP And MySQL

› Server Variables in ASP

› Create Tell A Friend Script With HTML & PHP

› CSS Cursors - How To Use Them

› How to export a GridView to Excel - Asp.net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.