This is a pretty simple one, but I think I had read of a few people having issue with the old leading zero issue when using the date in the filename.
Code: Select allfunction getDateTimeStr(){
var newDateTime = new Date();
var outDate = newDateTime.getFullYear().toString();
var theMonth = (newDateTime.getMonth()+1).toString();
outDate += ((theMonth.length<2)?"0":"")+theMonth;
theMonth = null;
var theDate = newDateTime.getDate().toString();
outDate += ((theDate.length<2)?"0":"")+theDate;
outDate += "_";
theDate = null;
var theHours = newDateTime.getHours().toString()
outDate += ((theHours.length<2)?"0":"")+theHours;
theHours = null;
var theMins = newDateTime.getMinutes().toString()
outDate += ((theMins.length<2)?"0":"")+theMins;
theMins = null;
newDateTime = null;
return outDate;
}
note: the year/month/date format is very valuable for sorting
Date and Time string for filename with leading zeros
Date and Time string for filename with leading zeros
I was doing something with dates the other day and found this function written by Xbytor very useful (I don't think I altered it, but I may have)
Code: Select allfunction toISODateString (date, timeDesignator, dateOnly) {
if (!date) date = new Date();
var str = '';
if (timeDesignator == undefined) { timeDesignator = 'T' };
function _zeroPad(val) { return (val < 10) ? '0' + val : val; }
if (date instanceof Date) {
str = date.getFullYear() + '-' +
_zeroPad(date.getMonth()+1,2) + '-' +
_zeroPad(date.getDate(),2)
if (!dateOnly) {
str += timeDesignator +
_zeroPad(date.getHours(),2) + ':' +
_zeroPad(date.getMinutes(),2) + ':' +
_zeroPad(date.getSeconds(),2);
}
}
return str;
}
Andrew
Code: Select allfunction toISODateString (date, timeDesignator, dateOnly) {
if (!date) date = new Date();
var str = '';
if (timeDesignator == undefined) { timeDesignator = 'T' };
function _zeroPad(val) { return (val < 10) ? '0' + val : val; }
if (date instanceof Date) {
str = date.getFullYear() + '-' +
_zeroPad(date.getMonth()+1,2) + '-' +
_zeroPad(date.getDate(),2)
if (!dateOnly) {
str += timeDesignator +
_zeroPad(date.getHours(),2) + ':' +
_zeroPad(date.getMinutes(),2) + ':' +
_zeroPad(date.getSeconds(),2);
}
}
return str;
}
Andrew
Date and Time string for filename with leading zeros
A good starting point for details on ISO 8601 is here http://en.wikipedia.org/wiki/ISO_8601.
Date and Time string for filename with leading zeros
in the end i brought it all together as
Code: Select allfunction getDateTimeStr(){
function _zeroPad(val,len){
val = val.toString();
len = (len)?len:2;
while(val.length<len){
val = "0" + val;
}
return val;
}
var newDateTime = new Date();
var outDate = newDateTime.getFullYear().toString();
outDate += _zeroPad(newDateTime.getMonth()+1,2);
outDate += _zeroPad(newDateTime.getDate(),2);
outDate += "T";
outDate += _zeroPad(newDateTime.getHours(),2);
outDate += _zeroPad(newDateTime.getMinutes(),2);
outDate += "-";
outDate += _zeroPad(newDateTime.getSeconds(),2);
outDate += "-";
outDate += _zeroPad(newDateTime.getMilliseconds(),3);
outDate += "+";
newDateTime = null;
return outDate;
}
This is specific to the current date and time for file naming, but could easily be modified to accept a date as a param.
Code: Select allfunction getDateTimeStr(){
function _zeroPad(val,len){
val = val.toString();
len = (len)?len:2;
while(val.length<len){
val = "0" + val;
}
return val;
}
var newDateTime = new Date();
var outDate = newDateTime.getFullYear().toString();
outDate += _zeroPad(newDateTime.getMonth()+1,2);
outDate += _zeroPad(newDateTime.getDate(),2);
outDate += "T";
outDate += _zeroPad(newDateTime.getHours(),2);
outDate += _zeroPad(newDateTime.getMinutes(),2);
outDate += "-";
outDate += _zeroPad(newDateTime.getSeconds(),2);
outDate += "-";
outDate += _zeroPad(newDateTime.getMilliseconds(),3);
outDate += "+";
newDateTime = null;
return outDate;
}
This is specific to the current date and time for file naming, but could easily be modified to accept a date as a param.