I have an application I'm building and I want to create a date object with a "plus string", similar to PHP's strtotime()
method.
Basically I want to be able to say
var date = new Date("+30 seconds");
Is there a way to create a date this way? I want it to be able to accept basic time values:
As far as I know even very popular libraries like moment.js don't implement this feature. Also this strtotime implementation for Javascript doesn't handle your input.
So I guess we'll have to write it ourselves. This could get you started:
var parsePlus = (function () {
"use strict";
var converter = {
'second': 1000,
'minute': 1000 * 60,
'hour': 1000 * 60 * 60
};
return function (input) {
var sign = (input[0] === "+" || input[0] !== "-") ? 1 : -1,
current = new Date().getTime();
var parts = input.match(/(\+|-)?(\d+)\s*(second|minute|hour)s?/i);
return new Date(current + sign * parts[2] * converter[parts[3]]);
}
})();
console.log(parsePlus("+30seconds"));
console.log(parsePlus("-1hour"));
the Date function accepts numerical values
new Date(2011, 0, 1, 2, 3, 4, 567) // 1 Jan 2011, 02:03:04.567 in local timezone
you could use a function that splits a string by whitespace and feeds it to the Date object constructor
function s2date(s){return new Date(s.split(' '))}
I went ahead and created a function myself since something else doesn't seem to exist.
function parseDate(date)
{
if(date == "now" || date == "")
{
return new Date();
}
if(date.charAt("0") == "+")
{
date.replace("+", "");
var cmp = date.split(" ");
if(cmp[1].indexOf("seconds") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 1000));
}
else if(cmp[1].indexOf("minutes") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 1000));
}
else if(cmp[1].indexOf("hours") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 1000));
}
else if(cmp[1].indexOf("days") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 1000));
}
else if(cmp[1].indexOf("weeks") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 7 * 1000));
}
else if(cmp[1].indexOf("months") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 30 * 1000));
}
else if(cmp[1].indexOf("years") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 365 * 1000));
}
}
if(date.charAt("0") == "-")
{
var cmp = date.split(" ");
if(cmp[1].indexOf("seconds") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 1000));
}
else if(cmp[1].indexOf("minutes") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 1000));
}
else if(cmp[1].indexOf("hours") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 1000));
}
else if(cmp[1].indexOf("days") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 1000));
}
else if(cmp[1].indexOf("weeks") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 7 * 1000));
}
else if(cmp[1].indexOf("months") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 30 * 1000));
}
else if(cmp[1].indexOf("years") > -1)
{
return new Date((new Date()).getTime() + (cmp[0] * 60 * 60 * 24 * 365 * 1000));
}
}
return new Date(date);
}
This accepts the following inputs:
"now" | "": returns current date
Datestring: returns corresponding date (this already worked in new Date(dateString))
[+ | -][#] [timevar]: returns current time [+ | -] [#] of [timevar]
e.g. "+30 seconds"
No. You can't do var date = new Date("+30 seconds");
But you can do something like:
var date = new Date( Date.now() + (30 * 1000) );
If you wanted to build that into a function, you'd have to split the string and determine how many milliseconds to add:
function AddDate(string, timestamp){
var s = string.split(' '), increaseby, newtime;
switch(s[1]){
case 'seconds':
increaseby = 1000; // milliseconds in one second
break;
case 'minutes':
increaseby = 1000 * 60; // milliseconds in a minute
break;
}
newtime = (+s[0]) * increaseby;
return timestamp + newtime;
}
(Or something like that.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.