Date functions in ASP

Source: Internet
Author: User
Tags date functions integer return trim
function | date <%
' Date function Package Datapackage
%>
<script language= "JavaScript" >
function isdatestring (STR)
<!--
{
Verifies that date-type data in YYYY-MM-DD format, the return value is true succeeds, otherwise returns false
Returns False if an empty string is passed
Define Separator
var ssplit= "-"

var iyearpos=str.indexof (ssplit);
if (iyearpos==-1) return false;

var imonthpos=str.indexof (ssplit,iyearpos+1);
if (imonthpos==-1) return false;

Isolate the Year
Syear=str.substr (0,iyearpos);
Separating out the month
Smonth=str.substr (IYEARPOS+1,IMONTHPOS-IYEARPOS-1);
Month length is two
if (smonth.length!=2) return false;
if (smonth.substr (0,1) = = "0") smonth=smonth.substr (1);
Isolate date
Sday=str.substr (imonthpos+1);
Day length is two
if (sday.length!=2) return false;
if (sday.substr (0,1) = = "0") sday=sday.substr (1);


Is the year a number?
if (isNaN (Syear)) return false;
var iyear=parseint (syear);
Is the year an integer?
if (syear!=iyear) return false;
Is the year between 1000-9999?
if (iyear<1000 | | iyear>9999) return FALSE;

Is the month a number?
if (isNaN (Smonth)) return false;
var imonth=parseint (Smonth);
Is the month an integer?
if (Smonth!=imonth) return false;
Is the month between 1-12?
if (imonth<1 | | imonth>12) return FALSE;

is the date a number?
if (isNaN (Sday)) return false;
var iday=parseint (Sday);
is the date an integer?
if (Sday!=iday) return false;
is the date between 1-31?
if (iday<1 | | iday>31) return FALSE;

if (iday<29) return true;

is the date data legal?
Switch (imonth)
{
Case 1:
if (iday>31) return false;
Break
Case 2:
if (Isleapyear (iyear))
{
if (iday>29) return false;
}
Else
{
if (iday>28) return false;
}
Break
Case 3:
if (iday>31) return false;
Break
Case 4:
if (iday>30) return false;
Break
Case 5:
if (iday>31) return false;
Break
Case 6:
if (iday>30) return false;
Break
Case 7:
if (iday>31) return false;
Break
Case 8:
if (iday>31) return false;
Break
Case 9:
if (iday>30) return false;
Break
Case 10:
if (iday>31) return false;
Break
Case 11:
if (iday>30) return false;
Break
Case 12:
if (iday>31) return false;
}

return true;

}


function Isleapyear (Y)
{
Year is a leap years

if (Y% 4!=0) return false;
if (Y% = = 0) return true;
if (Y% = = 0) return false;
}


-->
</script>


<%
'**********************
' Purpose: Convert date to standard date string
' Input: Date
' Output: Date string, such as: 2002-12-22
'**********************
Function Getdatestr (ddate)
Dim strtmp
Getdatestr = DatePart ("yyyy", ddate)
strtmp = Trim (DatePart ("M", Ddate))
If Len (strtmp) =1 then strtmp= "0" & strtmp
Getdatestr = getdatestr & "-" & strtmp
strtmp = Trim (DatePart ("D", Ddate))
If Len (strtmp) =1 then strtmp= "0" & strtmp
Getdatestr = getdatestr & "-" & strtmp
End Function
'**********************


'**********************
' Purpose: Convert date time to standard date time string
' Input: Date time
' Output: Date time string, such as: 2002-12-22
'**********************
Function Gettimestr (ddate)
Dim strtmp
Gettimestr = DatePart ("yyyy", ddate)
strtmp = Trim (DatePart ("M", Ddate))
If Len (strtmp) =1 then strtmp= "0" & strtmp
Gettimestr = gettimestr & "-" & strtmp
strtmp = Trim (DatePart ("D", Ddate))
If Len (strtmp) =1 then strtmp= "0" & strtmp
Gettimestr = gettimestr & "-" & strtmp
Gettimestr = Gettimestr & "" & Trim (DatePart ("H", ddate))
Gettimestr = gettimestr & ":" & Trim (DatePart ("M", Ddate))
End Function
'**********************


'**********************
' Objective: To calculate the total number of days in January
' Input: Year, month
' Output: Days
'**********************
function Getmonthdays (myyear,mymonth)
Dim thismonthfirday,nextmonthfirday
' Date of 1st of the current month
Thismonthfirday=myyear & "-" & Mymonth & "-1"
' Get the date of 1st next month
Nextmonthfirday=dateadd ("M", 1,thismonthfirday)
' Get the number of days of the current month
Getmonthdays=datediff ("D", Thismonthfirday,nextmonthfirday)
End Function
'**********************


'**********************
' Purpose: To return the formatted date
' Input: Any type parameter
' Output: Formatted date
'**********************
Function Formatdateex (D)


Dim vartemp

If IsDate (D) Then
Vartemp= "'" & D & ""
Else
vartemp= "NULL"
End If

Formatdateex=vartemp


End Function
'**********************


' Datepackage End
%>

Related Article

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.