You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将new Date()获取的时间固定为当日09:30:00?

解决方案:将当前日期时间固定为当天09:30:00

没问题,你可以通过调整JavaScript的Date对象属性轻松实现这个需求,以下两种方法都能帮你得到当天的09:30:00,同时保留印度标准时区(GMT+0530):

方法一:直接修改现有Date对象的时分秒

你可以用setHours()方法一次性设置小时、分钟、秒和毫秒,这个方法会自动保留原Date对象的时区信息:

var today = new Date();
// 设置为当天9点30分0秒0毫秒
today.setHours(9, 30, 0, 0);
console.log(today);
// 输出示例:Sat May 30 2020 09:30:00 GMT+0530 (India Standard Time)

方法二:基于当前日期创建新的Date对象

如果不想修改原有的today变量,可以提取当前的年、月、日,再结合指定的时分秒创建新的Date对象:

var today = new Date();
var today_morning = new Date(
  today.getFullYear(),
  today.getMonth(),
  today.getDate(),
  9,  // 目标小时
  30, // 目标分钟
  0,  // 目标秒数
  0   // 目标毫秒数
);
console.log(today_morning);
// 输出示例:Sat May 30 2020 09:30:00 GMT+0530 (India Standard Time)

这两种方法都会遵循你当前环境的时区设置,所以最终输出结果会带有GMT+0530 (India Standard Time)的标识,完全符合你的需求。

内容的提问来源于stack exchange,提问作者user2828442

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:37:48