Manage Learn to apply best practices and optimize your operations.

Displaying Dates In Multiple Formats From Us Format

Displaying Dates is particularly frustrating in a Web Browser and this
JavaScript demonstrates how to this.



&LTHEAD>

&LTSCRIPT LANGUAGE="JavaScript">

<!-- Begin

//Globals...
var dateStr;
var dateDisp;
var seldate;
var dispHTML = new String();
var dispStr = new String();

function displayInfo(dispH) {
dispHTML = dispH
//alert(dispH);
document.all('disp').innerHTML = dispH;

}


function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// Also separates date into month, day, and year variables

var day = (1000*60*60*24);
var todaydt = new Date();
var tomorrowdt = new Date();
var yesterdaydt = new Date();
//var dateStr = new String(document.forms[0].name.value)

tomorrowdt.setTime(todaydt.getTime()+ day);
yesterdaydt.setTime(todaydt.getTime()- day);

var today = todaydt.getMonth()+1+"/"+todaydt.getDate()+"/"+todaydt.getYear();
var tomorrow =
tomorrowdt.getMonth()+1+"/"+tomorrowdt.getDate()+"/"+tomorrowdt.getYear();
var yesterday =
yesterdaydt.getMonth()+1+"/"+yesterdaydt.getDate()+"/"+yesterdaydt.getYear();


if (dateStr.toLowerCase() == "today") {
dateStr = today
}

if (dateStr.toLowerCase() == "tomorrow") {
dateStr = tomorrow
}

if (dateStr.toLowerCase() == "yesterday") {
dateStr = yesterday
}

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}

month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
dateStr = (year+ "," + month + "," + day)

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}

dispHTML = dispHTML + "&LTP>&LTi> ...and date is ---> " + dateStr + " </i></P>";
return dateStr; // date is valid
}



function moveTime(days) {
//alert(seldate + " -" + days);
var newDate = new Date(seldate.getTime() + (60*60*24*days*1000));
dateStr = newDate.getYear()+","+(newDate.getMonth()+1)+","+newDate.getDate()
//alert(dateStr + " - " + newDate)
dispHTML = dispHTML + "&LTP> ...adjusted date is: " +
newDate.toLocaleString() + " by " +days+" day/s!</P>";
//return(dateObject)
};



function dateFormatForDisplay (dateStr,dformat) {

var formatOkay = "True"
var dispFormat = dformat.toLowerCase();
var FLength = dispFormat.length;

var charD = -1; // character d
var charM = -1; // character m
var charY = -1; // character y
var charW = -1; // character w
var charS1 = -1; // character space
var charS2 = -1; // character dash
var charS3 = -1; // character slash
var cntD = 0;
var cntM = 0;
var cntY = 0;
var cntW = 0;
var cntS1 = 0;
var cntS2 = 0;
var cntS3 = 0;
var dispS1 = "";
var dispS2 = "";
var dispS3 = "";


// check the layout for display

for (var i = 0; i <= (FLength-1); i++){

switch (dispFormat.substring(i,i+1)){

case "d":
if ( charD == -1 ){
charD = i
}
cntD = cntD
This was last published in November 2000

Dig Deeper on Lotus Notes Domino Administration Tools

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchExchange

SearchContentManagement

Close