Action

Insert Timestamp at Cursor

Posted by hahaSweat, Last update over 1 year ago - Unlisted

在光标处插入 202310301501 类型的时间戳

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    // 获取当前日期和时间
    var now = new Date();
    
    // 获取年、月、日、小时、分钟,并将它们格式化为需要的格式
    var year = now.getFullYear();
    // 月份从0开始,所以加1
    // 使用padStart方法确保它总是两位数(例如,“02”代表2月)。
    var month = String(now.getMonth() + 1).padStart(2, '0');  
    var day = String(now.getDate()).padStart(2, '0');
    var hour = String(now.getHours()).padStart(2, '0');
    var minute = String(now.getMinutes()).padStart(2, '0');
    
    // 合并以上信息以创建时间戳
    var timeStamp = year + month + day + hour + minute;
    
    // 获取当前光标的位置
    var cursorPosition = editor.getSelectedRange()[0];
    
    // 在当前光标位置插入时间戳
    draft.content = draft.content.slice(0, cursorPosition) + timeStamp + draft.content.slice(cursorPosition);
    
    // 将光标位置放在插入的时间戳后面
    editor.setSelectedRange(cursorPosition + timeStamp.length, 0);
    

Options

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