Action

Insert Date

Posted by @denismunhoz, Last update about 6 years ago

Show menu and insert date and time in the chosen format.

Steps

  • script

    var optDt = ["dd mm yyyy", "yyyy mm dd", "mm dd yyyy", "[none]"];
    var optDtSeparator = ["/", "-", ".", "[space]", "[none]"];
    var optTm = ["hh mm", "hh mm ss", "[none]"];
    var optTmSeparator = [":", "-", ".", "[space]", "[none]"];
    
    var selectedOptDt = ["dd mm yyyy"];
    var selectedOptTm = ["hh mm"];
    var selectedOptDtSeparator = ["/"];
    var selectedOptTmSeparator = [":"];
    
    var p = Prompt.create();
    
    p.title = "Insert date";
    p.message = "Select format of date and time (one of these or both) and separator.";
    p.addSelect("valueDt", "Date format:", optDt, selectedOptDt, false);
    p.addSelect("valueTm", "Time format:", optTm, selectedOptTm, false);
    p.addSelect("valueDtSeparator", "Date separator:", optDtSeparator, selectedOptDtSeparator, false);
    p.addSelect("valueTmSeparator", "Time separator:", optTmSeparator, selectedOptTmSeparator, false);
    p.addButton("OK");
    
    var curDate = new Date();
    var curDay = curDate.getDate();
    var curMonth = curDate.getMonth()+1;
    var curYear = curDate.getFullYear();
    var curHour = curDate.getHours();
    var curMinute = curDate.getMinutes();
    var curSecond = curDate.getSeconds();
    
    if (curDay < 10) {
    	curDay = "0" + curDay;
    }
    if (curMonth < 10) {
    	curMonth = "0" + curMonth;
    }
    if (curHour < 10) {
    	curHour = "0" + curHour;
    }
    if (curMinute < 10) {
    	curMinute = "0" + curMinute;
    }
    if (curSecond < 10) {
    	curSecond = "0" + curSecond;
    }
    
    if (p.show()) {
    	var txtFinal = "";
    	var dtSeparator = p.fieldValues["valueDtSeparator"];
    	var tmSeparator = p.fieldValues["valueTmSeparator"];
    	if (dtSeparator == "[space]") {
    		dtSeparator = " ";
    	}
    	if (dtSeparator == "[none]") {
    		dtSeparator = "";
    	}
    	if (tmSeparator == "[space]") {
    		tmSeparator = " ";
    	}
    	if (tmSeparator == "[none]") {
    		tmSeparator = "";
    	}
    	if (p.fieldValues["valueDt"].length !== 0) {
    		if (p.fieldValues["valueDt"] == "dd mm yyyy") {
    			txtFinal = curDay + dtSeparator + curMonth + dtSeparator + curYear + " ";
    		}
    		if (p.fieldValues["valueDt"] == "yyyy mm dd") {
    			txtFinal = curYear + dtSeparator + curMonth + dtSeparator + curDay + " ";
    		}
    		if (p.fieldValues["valueDt"] == "mm dd yyyy") {
    			txtFinal = curMonth + dtSeparator + curDay + dtSeparator + curYear + " ";
    		}
    		if (p.fieldValues["valueDt"] == "[none]") {
    			txtFinal = "";
    		}
    	}
    	if (p.fieldValues["valueTm"].length !== 0) {
    		if (p.fieldValues["valueTm"] == "hh mm ss") {
    			txtFinal += curHour + tmSeparator + curMinute + tmSeparator + curSecond + " ";
    		}
    		if (p.fieldValues["valueTm"] == "hh mm") {
    			txtFinal += curHour + tmSeparator + curMinute + " ";
    		}
    		if (p.fieldValues["valueTm"] == "[none]") {
    			txtFinal += "";
    		}
    	}
    	var selRange = editor.getSelectedRange();
    	editor.setSelectedText(txtFinal);
    	editor.setSelectedRange(selRange[0]+txtFinal.length,0);
    	editor.activate();
    }
    else {
    	context.cancel("cancelled by user");
    }

Options

  • After Success Nothing
    Notification None
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.