get_current_time
Retrieve current time with customizable date formats, offsets, and future date options for scheduling and time-sensitive applications.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | 日期格式 | iso |
| days_offset | No | 日期偏移量 | 0 |
| return_future_dates | No | 是否返回未来日期 | false |
| future_days | No | 未来天数 | 7 |
Implementation Reference
- src/index.js:391-571 (handler)The handler function for the 'get_current_time' tool. It parses input arguments, computes current time and offset date in multiple formats (ISO, slash, Chinese, timestamp, full), weekdays, builds comprehensive date info, generates travel date suggestions and hotel stay suggestions, optionally generates future dates list, and returns JSON stringified result.async (args) => { const now = new Date(); // 将字符串参数转换为对应的数据类型 let daysOffsetInt; try { daysOffsetInt = args.days_offset ? parseInt(args.days_offset, 10) : 0; } catch (e) { return { content: [{ type: 'text', text: JSON.stringify({ error: 'days_offset必须是整数' }) }], isError: true }; } const returnFutureDatesBool = args.return_future_dates ? args.return_future_dates.toLowerCase() === 'true' : false; let futureDaysInt; try { futureDaysInt = args.future_days ? parseInt(args.future_days, 10) : 7; } catch (e) { return { content: [{ type: 'text', text: JSON.stringify({ error: 'future_days必须是整数' }) }], isError: true }; } // 计算目标日期 const targetDate = new Date(now); targetDate.setDate(targetDate.getDate() + daysOffsetInt); // 格式化日期的辅助函数 const formatDate = (date, format) => { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); switch (format) { case 'iso': return `${year}-${month}-${day}`; case 'slash': return `${day}/${month}/${year}`; case 'chinese': return `${year}年${month}月${day}日`; case 'timestamp': return Math.floor(date.getTime() / 1000); case 'full': return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; default: return `${year}-${month}-${day}`; } }; // 获取星期几 const getWeekday = (date, short = false) => { const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; const shortWeekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; return short ? shortWeekdays[date.getDay()] : weekdays[date.getDay()]; }; // 构建结果对象 const result = { now: { iso: formatDate(now, 'iso'), slash: formatDate(now, 'slash'), chinese: formatDate(now, 'chinese'), timestamp: formatDate(now, 'timestamp'), full: formatDate(now, 'full'), time: `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`, weekday: getWeekday(now), weekday_short: getWeekday(now, true), year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate(), hour: now.getHours(), minute: now.getMinutes(), second: now.getSeconds() }, target_date: { iso: formatDate(targetDate, 'iso'), slash: formatDate(targetDate, 'slash'), chinese: formatDate(targetDate, 'chinese'), timestamp: formatDate(targetDate, 'timestamp'), full: formatDate(targetDate, 'full'), time: `${String(targetDate.getHours()).padStart(2, '0')}:${String(targetDate.getMinutes()).padStart(2, '0')}:${String(targetDate.getSeconds()).padStart(2, '0')}`, weekday: getWeekday(targetDate), weekday_short: getWeekday(targetDate, true), year: targetDate.getFullYear(), month: targetDate.getMonth() + 1, day: targetDate.getDate(), hour: targetDate.getHours(), minute: targetDate.getMinutes(), second: targetDate.getSeconds() } }; // 旅行场景常用日期 const tomorrow = new Date(now); tomorrow.setDate(tomorrow.getDate() + 1); const nextWeek = new Date(now); nextWeek.setDate(nextWeek.getDate() + 7); const nextMonth = new Date(now); nextMonth.setDate(nextMonth.getDate() + 30); // 计算下一个周五(周末开始) const weekend = new Date(now); weekend.setDate(weekend.getDate() + ((5 - weekend.getDay() + 7) % 7)); // 计算下一个周日(周末结束) const weekendEnd = new Date(now); weekendEnd.setDate(weekendEnd.getDate() + ((0 - weekendEnd.getDay() + 7) % 7)); result.travel_dates = { today: formatDate(now, 'iso'), tomorrow: formatDate(tomorrow, 'iso'), next_week: formatDate(nextWeek, 'iso'), next_month: formatDate(nextMonth, 'iso'), weekend: formatDate(weekend, 'iso'), weekend_end: formatDate(weekendEnd, 'iso') }; // 对于旅行预订常用的入住-退房日期对 const tomorrow2Days = new Date(tomorrow); tomorrow2Days.setDate(tomorrow2Days.getDate() + 2); const tomorrow7Days = new Date(tomorrow); tomorrow7Days.setDate(tomorrow7Days.getDate() + 7); const nextMonth2Days = new Date(nextMonth); nextMonth2Days.setDate(nextMonth2Days.getDate() + 2); result.hotel_stay_suggestions = [ { check_in: formatDate(tomorrow, 'iso'), check_out: formatDate(tomorrow2Days, 'iso'), nights: 2, description: '短暂周末度假' }, { check_in: formatDate(tomorrow, 'iso'), check_out: formatDate(tomorrow7Days, 'iso'), nights: 7, description: '一周度假' }, { check_in: formatDate(nextMonth, 'iso'), check_out: formatDate(nextMonth2Days, 'iso'), nights: 2, description: '下个月短暂出行' } ]; // 如果需要一系列未来日期 if (returnFutureDatesBool) { const futureDates = []; for (let i = 0; i < futureDaysInt; i++) { const futureDate = new Date(now); futureDate.setDate(futureDate.getDate() + i); futureDates.push({ iso: formatDate(futureDate, 'iso'), slash: formatDate(futureDate, 'slash'), chinese: formatDate(futureDate, 'chinese'), weekday: getWeekday(futureDate), weekday_short: getWeekday(futureDate, true), days_from_now: i }); } result.future_dates = futureDates; } // 使用请求的格式作为主要返回值 result.date = formatDate(targetDate, args.format || 'iso'); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; }
- src/index.js:385-390 (schema)Zod schema for input parameters of the get_current_time tool: format (default 'iso'), days_offset (default '0'), return_future_dates (default 'false'), future_days (default '7').{ format: z.string().default('iso').describe('日期格式'), days_offset: z.string().default('0').describe('日期偏移量'), return_future_dates: z.string().default('false').describe('是否返回未来日期'), future_days: z.string().default('7').describe('未来天数') },
- src/index.js:384-572 (registration)Registration of the 'get_current_time' tool using server.tool(name, inputSchema, handlerFn).'get_current_time', { format: z.string().default('iso').describe('日期格式'), days_offset: z.string().default('0').describe('日期偏移量'), return_future_dates: z.string().default('false').describe('是否返回未来日期'), future_days: z.string().default('7').describe('未来天数') }, async (args) => { const now = new Date(); // 将字符串参数转换为对应的数据类型 let daysOffsetInt; try { daysOffsetInt = args.days_offset ? parseInt(args.days_offset, 10) : 0; } catch (e) { return { content: [{ type: 'text', text: JSON.stringify({ error: 'days_offset必须是整数' }) }], isError: true }; } const returnFutureDatesBool = args.return_future_dates ? args.return_future_dates.toLowerCase() === 'true' : false; let futureDaysInt; try { futureDaysInt = args.future_days ? parseInt(args.future_days, 10) : 7; } catch (e) { return { content: [{ type: 'text', text: JSON.stringify({ error: 'future_days必须是整数' }) }], isError: true }; } // 计算目标日期 const targetDate = new Date(now); targetDate.setDate(targetDate.getDate() + daysOffsetInt); // 格式化日期的辅助函数 const formatDate = (date, format) => { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); switch (format) { case 'iso': return `${year}-${month}-${day}`; case 'slash': return `${day}/${month}/${year}`; case 'chinese': return `${year}年${month}月${day}日`; case 'timestamp': return Math.floor(date.getTime() / 1000); case 'full': return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; default: return `${year}-${month}-${day}`; } }; // 获取星期几 const getWeekday = (date, short = false) => { const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; const shortWeekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; return short ? shortWeekdays[date.getDay()] : weekdays[date.getDay()]; }; // 构建结果对象 const result = { now: { iso: formatDate(now, 'iso'), slash: formatDate(now, 'slash'), chinese: formatDate(now, 'chinese'), timestamp: formatDate(now, 'timestamp'), full: formatDate(now, 'full'), time: `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`, weekday: getWeekday(now), weekday_short: getWeekday(now, true), year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate(), hour: now.getHours(), minute: now.getMinutes(), second: now.getSeconds() }, target_date: { iso: formatDate(targetDate, 'iso'), slash: formatDate(targetDate, 'slash'), chinese: formatDate(targetDate, 'chinese'), timestamp: formatDate(targetDate, 'timestamp'), full: formatDate(targetDate, 'full'), time: `${String(targetDate.getHours()).padStart(2, '0')}:${String(targetDate.getMinutes()).padStart(2, '0')}:${String(targetDate.getSeconds()).padStart(2, '0')}`, weekday: getWeekday(targetDate), weekday_short: getWeekday(targetDate, true), year: targetDate.getFullYear(), month: targetDate.getMonth() + 1, day: targetDate.getDate(), hour: targetDate.getHours(), minute: targetDate.getMinutes(), second: targetDate.getSeconds() } }; // 旅行场景常用日期 const tomorrow = new Date(now); tomorrow.setDate(tomorrow.getDate() + 1); const nextWeek = new Date(now); nextWeek.setDate(nextWeek.getDate() + 7); const nextMonth = new Date(now); nextMonth.setDate(nextMonth.getDate() + 30); // 计算下一个周五(周末开始) const weekend = new Date(now); weekend.setDate(weekend.getDate() + ((5 - weekend.getDay() + 7) % 7)); // 计算下一个周日(周末结束) const weekendEnd = new Date(now); weekendEnd.setDate(weekendEnd.getDate() + ((0 - weekendEnd.getDay() + 7) % 7)); result.travel_dates = { today: formatDate(now, 'iso'), tomorrow: formatDate(tomorrow, 'iso'), next_week: formatDate(nextWeek, 'iso'), next_month: formatDate(nextMonth, 'iso'), weekend: formatDate(weekend, 'iso'), weekend_end: formatDate(weekendEnd, 'iso') }; // 对于旅行预订常用的入住-退房日期对 const tomorrow2Days = new Date(tomorrow); tomorrow2Days.setDate(tomorrow2Days.getDate() + 2); const tomorrow7Days = new Date(tomorrow); tomorrow7Days.setDate(tomorrow7Days.getDate() + 7); const nextMonth2Days = new Date(nextMonth); nextMonth2Days.setDate(nextMonth2Days.getDate() + 2); result.hotel_stay_suggestions = [ { check_in: formatDate(tomorrow, 'iso'), check_out: formatDate(tomorrow2Days, 'iso'), nights: 2, description: '短暂周末度假' }, { check_in: formatDate(tomorrow, 'iso'), check_out: formatDate(tomorrow7Days, 'iso'), nights: 7, description: '一周度假' }, { check_in: formatDate(nextMonth, 'iso'), check_out: formatDate(nextMonth2Days, 'iso'), nights: 2, description: '下个月短暂出行' } ]; // 如果需要一系列未来日期 if (returnFutureDatesBool) { const futureDates = []; for (let i = 0; i < futureDaysInt; i++) { const futureDate = new Date(now); futureDate.setDate(futureDate.getDate() + i); futureDates.push({ iso: formatDate(futureDate, 'iso'), slash: formatDate(futureDate, 'slash'), chinese: formatDate(futureDate, 'chinese'), weekday: getWeekday(futureDate), weekday_short: getWeekday(futureDate, true), days_from_now: i }); } result.future_dates = futureDates; } // 使用请求的格式作为主要返回值 result.date = formatDate(targetDate, args.format || 'iso'); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } );