如何从javascript中的日期中减去分钟数? [英] How do I subtract minutes from a date in javascript?

查看:105
本文介绍了如何从javascript中的日期中减去分钟数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此伪代码转换为工作js [不要担心结束日期的来源,除非它是一个有效的javascript日期]。

How can I translate this pseudo code into working js [don't worry about where the end date comes from except that it's a valid javascript date].

var myEndDateTime = somedate;  //somedate is a valid js date  
var durationInMinutes = 100; //this can be any number of minutes from 1-7200 (5 days)

//this is the calculation I don't know how to do
var myStartDate = somedate - durationInMuntes;

alert("The event will start on " + myStartDate.toDateString() + " at " + myStartDate.toTimeString());


推荐答案

一旦你知道这一点:

  • You can create a Date by calling the constructor with milliseconds since Jan 1, 1970.
  • The valueOf() a Date is the number of milliseconds since Jan 1, 1970
  • There are 60,000 milliseconds in a minute :-]

......它不是那么难。

...it isn't so hard.

在下面的代码中,通过从<$ c减去适当的毫秒数来创建新的 Date $ c> myEndDateTime :

In the code below, a new Date is created by subtracting the appropriate number of milliseconds from myEndDateTime:

var MS_PER_MINUTE = 60000;
var myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);

这篇关于如何从javascript中的日期中减去分钟数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆