Important alert: (current site time 7/15/2013 1:44:10 PM EDT)
 

VB icon

VBScript's DateSerial Function

Email
Submitted on: 6/15/2003 11:49:17 PM
By: Randy McCleary 
Level: Advanced
User Rating: By 9 Users
Compatibility: JavaScript
Views: 14239
(About the author)
 
     DateSerial is a function that is supplied in VBScript, and Javascript doesn't have a function like this, so I created a function to work in Javascript to do the same as the VBScript function does. I hope you all find this useful. I'm working on a hold Script Library with all the VBScript Functions converted into Javascript. I'm about 90% Completed.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: VBScript's DateSerial Function
' Description:DateSerial is a function that is supplied in VBScript, and Javascript doesn't have a function like this, so I created a function to work in Javascript to do the same as the VBScript function does. I hope you all find this useful. I'm working on a hold Script Library with all the VBScript Functions converted into Javascript. I'm about 90% Completed.
' By: Randy McCleary
'
' Inputs:Year, Month, Day
'
' Returns:Date in the format as MM/DD/YYYY
'
' Side Effects:None, that are know of.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3637&lngWId=14'for details.'**************************************

/**************************************************************
 DateSerial: This function returns the current Date of the computer.
 Parameters:
 Year : String, Integer, or Expression (100 To 9999)
 Month : String, Integer, or Expression (-9999999 To +9999999)
 Day: String, Integer, or Expression (-9999999 To +9999999)
 Returns: Returns the Date. (6/22/2003)
***************************************************************/
function DateSerial(Year, Month, Day) {
	var Years= new Number(Year);
	var Months = new Number(Month);
	var Days= new Number(Day);
	var arrDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	//***************************************************
	// Years
	//***************************************************
	if ((Years >= 100) && (Years <= 9999)) {
		Years = Years;
	}
	else {
		return null;
	}
	//***************************************************
	// Months
	//***************************************************
	if (Months >= 1 && Months <= 12) {
		Months = Months;
	}
	else if (Months > 12) {
		// While Months is greater than 12, then keep subtracting 12 
		// to Months and incrementing the year by one.
		while (Months > 12 ) { 
			Months -= 12;
			Years += 1;
		}
	}
	else if (Months < 1) {
		// While Months is less than - 12, then keep adding 12 to Months
		// and decrementing the year by one.
		while (Months <= -12 ) { 
			Months += 12;
			Years -= 1;
		}
		
		// If Months is greater than -12 and is Greater or Equal to 0
		// then subtract One from Years.
		if (Months > -12 && Months <= 0) {
			Years -= 1;
		}
		
		// Months = 12 plus what is left over in the variable Months
		Months = 12 + Months;
	}
	//***************************************************
	// Days
	//***************************************************
	if (Days > 0) {
		// Set DaysInMonth for Feburary
		arrDays[1] = DaysInMonth(2, Years);
	
		// While Days is greater than the total days in the month subtract
		// the total days from the Days, and increment the Months by 1.
		while (Days > arrDays[Months - 1]) {
			Days -= arrDays[Months -1];
			Months += 1;		
			
			if (Months > 12) {
				Months -= 12;
				Years += 1;
			}
			
			// Set DaysInMonth for Feburary
			arrDays[1] = DaysInMonth(2, Years);
		}
	}
	else if (Days <= 0) {
		if (Days == 0) {
			Months -=1;
			if (Months == 0){
				Months = 12;
				Years -= 1;
			}
			
			// Set DaysInMonth for Feburary
			arrDays[1] = DaysInMonth(2, Years);
			Days = arrDays[Months-1];
		}
		else {
			arrDays[1] = DaysInMonth(2, Years);
			
			// While Days is less than or equal to the total days in the Month
			// then increment the days by the total days in the month and 
			// subtract one from the Months.
			while (Days <= - arrDays[Months - 1]) {
				// Set DaysInMonth for Feburary
				arrDays[1] = DaysInMonth(2, Years);
				Days += arrDays[Months -1];
				Months -= 1;
				
				if (Months == 0) {
					Months += 12;
					Years -= 1;
				}		
				
				// Set DaysInMonth for Feburary
				arrDays[1] = DaysInMonth(2, Years);				
			}
			if (Days == 0 || Days > -arrDays[Months-1]) {
				Months -=1;
			}
			if (Months == 0) {
				Months += 12;
				Years -= 1;
			}
			
			// Set DaysInMonth for Feburary
			arrDays[1] = DaysInMonth(2, Years);	
			
			if (Days == -arrDays[Months-1]) {
				Days = 0;
				Months -=1;
			}
			if (Months == 0) {
				Months += 12;
				Years -= 1;
			}
			
			// Set DaysInMonth for Feburary
			arrDays[1] = DaysInMonth(2, Years);
			
			// Days is equal to the number of days in the month to 
			// the days left over not greater then the total days 
			// in the month. Ex: Jan 31 days - 15 = Jan 16th.
			Days = arrDays[Months-1] + Days;
		}
	}
	if ((Years >= 1) && (Years <= 9999)) {
		return Months + "/" + Days + "/" + Years;
	}
	else {
		return null;
	}
}
/**************************************************************
 DaysInMonth: This function returns the number of days in a
 	a given month and year. This is used to find out
 	when a year has a leap day or not.
 Parameters:
Month = A month Number (1-12).
Year = A 4 Digit Year number.
 Returns: Integer - (28-31).
***************************************************************/
function DaysInMonth(Month, Year) {
	var Days;
	var arrDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	if ((Month==2) && (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))) {
		arrDays[1] = 29;
	}
	return arrDays[Month-1];
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.