If there are spaces or other delimiters in the string, you can split the Split method, return an array, and
The number of years in the array is passed as a parameter to the new date () to generate the date type;
If there is no special separator, you can use the substr (X,y) method to take a substring to generate a date, substr (x,y
X represents the starting position of the string, and Y indicates the length of the substring!
If the string is "/" split, it can be converted using Date.parse (DATESTR)
<mce:script language= "JavaScript" ><!--
function Stringtodate (DATESTR)
{
var converted = Date.parse (' 2009/01/05 ');
alert (converted);
Alert (DATESTR.SUBSTR (0,4) + "/" +datestr.substr (5,2) + "/" +datestr.substr (8,2));
var mydate = new Date (converted);
alert (mydate);
Alert (mydate.getfullyear () + "/" + (Mydate.getmonth () +1) + "/" +mydate.getdate
());
if (isNaN (DATESTR))
{
var Delimcahar = datestr.indexof ('/')!=-1? ' /':'-';
Datestr = "2008-08-08";
var arys= datestr.split ('-');
var d = new Date (Arys[0], arys[1], arys[2]);
alert (d);
}
alert (mydate);
return mydate;
}
--></mce:script>
The use of regular processing
New Date (A + "/" + B + "/" + C). getmonth () = = B-1
A, B, C can be (or even null, Boolean) as long as they are variables that can be converted to numbers, and you can use the new
Date (Y,M,D) creates a new date format,
And according to your request, new Date (A,b,c). getmonth () = = B-1 is true, otherwise it is not satisfied
function IsDate (dateValue) {
var regex = new RegExp ("^ (?:(?:( [0-9] {4} (-|/) (?:(?: 0? [1,3-9]|1[0-2]) (-|/)
(?: 29|30) | ((?: 0? [13578]|1[02]) (-|/) 31)) | ([0-9]{4} (-|/) (?: 0?) [1-9]|1[0-2]) (-
|/) (?: 0? [1-9]|1d|2[0-8]) | (((?:( DD (?: 0 [48]| [2468] [048]| [13579] [26]) | (?: 0
[48]00| [2468] [048]00| [13579] [26]00)) (-|/) 0?2 (-|/) 29))));
if (!regex.test (DateValue))
return false;
Else
return true;
}
Pass it over to judge
Instance
function Checkdate (text) {
if (!text) return false;
Text = Text.replace (/[/-]0?/g, "/");
if (!text.match (/^d{4}/d{1,2}/d{1,2}$/)) return false;
var d = new Date (text);
return [D.getfullyear (), D.getmonth () + 1, d.getdate ()].join ("/") = = text;
}
Document.open ();
var datas = ["", "2010-01-01", "2010/02/30", "2010/02", "2010/22/30",
"2012/2/29", "2012/02/29"];
for (var i = 0; i < datas.length; i++) {
document.write (Datas[i], "=", Checkdate (Datas[i)), "<br/>");
}
Document.close ();
Output
Assembly Code
=false
2010-01-01=true
2010/02/30=false
2010/02=false
2010/22/30=false
2012/2/29=true
2012/02/29=true
Common methods
<script language= "Web Effects" >
e///////////////////////
Get the current date, format YYYY-MM-DD
////////////////////////////////////////////////////////
function Getcurrentdate ()
{
var year=0;
var month=0;
var day=0;
var currentdate = new Date ();
Return changedatetostring (currentdate);
}
e///////////////////////
Get the current date, format Yyyy-mm-dd hh:mm
////////////////////////////////////////////////////////
function GetCurrentTime ()
{
var year=0;
var month=0;
var day=0;
var currentdate = new Date ();
Return changetimetostring (currentdate);
}
////////////////////////////////////////////////////////
Converts a date type to a string format YYYY-MM-DD
////////////////////////////////////////////////////////
function changedatetostring (Datein)
{
var year=0;
var month=0;
var day=0;
var currentdate= "";
Initialization time
Year = Datein.getyear ();
Month = Datein.getmonth () +1;
Day = Datein.getdate ();
currentdate = year + "-";
if (Month >= 10)
{
currentdate = currentdate + Month + "-";
}
Else
{
currentdate = currentdate + "0" + Month + "-";
}
if (Day >= 10)
{
currentdate = currentdate + day;
}
Else
{
currentdate = currentdate + "0" + day;
}
return currentdate;
}
///////////////////////////////////////////////////////
Converts a date type to a string format Yyyy-mm-dd hh:mm
////////////////////////////////////////////////////////
function changetimetostring (Datein)
{
var year=0;
var month=0;
var day=0;
var Hour = 0;
var Minute = 0;
var currentdate= "";
Initialization time
Year = Datein.getyear ();
Month = Datein.getmonth () +1;
Day = Datein.getdate ();
Hour = Datein.gethours ();
Minute = Datein.getminutes ();
currentdate = year + "-";
if (Month >= 10)
{
currentdate = currentdate + Month + "-";
}
Else
{
currentdate = currentdate + "0" + Month + "-";
}
if (Day >= 10)
{
currentdate = currentdate + day;
}
Else
{
currentdate = currentdate + "0" + day;
}
if (Hour >=10)
{
currentdate = currentdate + "" + Hour;
}
Else
{
currentdate = currentdate + "0" + Hour;
}
if (Minute >=10)
{
currentdate = currentdate + ":" + Minute;
}
Else
{
currentdate = currentdate + ": 0" + Minute;
}
return currentdate;
}
</script>