I need to write a script to update a time. I want to make it increase by 20 seconds so
"Jun 23, 2011 12:00 AM"
becomes
"Jun 23, 2011 12:20 AM"
How can I do that?
The easiest way to manipulate dates is with the Date.js library available at http://datejs.com/. Then it would be:
var d = Date.parse("Jun 23, 2011 12:00 AM").add(20).seconds();
Or if d is already set to a date, you could simply do:
var d = new Date(d.getTime() + 20000);
var seconds = 1000; // 1 second = 1000 milliseconds
var originalDate = Date.parse("Jun 23, 2011 12:00 AM");
var newDate = new Date(originalDate + 2 * seconds);
Related
Fri Nov 10 05:45:36 +0000 2017
I've tried both moment and chrono-node. Both are getting stumped by this date format.
Any suggestions to get a valid UTC date?
Thanks
To add to #CertainPerformance's answer, the problem with your code, if you try with MomentJS is that it is not a standard ISO date string. Parsing it directly without specifying format will result in incorrect result and a warning like this:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release.
To mitigate, always pass the format to MomentJS constructor like this:
const inputStr = "Fri Nov 10 05:45:36 +0000 2017"
const mom = moment(inputStr, 'ddd MMM D HH:mm:ss ZZ YYYY');
console.log(mom.toISOString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
I think this should help you out:
var date = new Date("Fri Nov 10 05:45:36 +0000 2017").toISOString();
date = date.split("T")[0];
console.log(date);
Moment works just fine:
const inputStr = "Fri Nov 10 05:45:36 +0000 2017"
const mom = moment(inputStr, 'ddd MMM D HH:mm:ss ZZ YYYY');
console.log(mom.toISOString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
You can see the difference between 2 items of each array which is returned by the code that I have written to process the date strings of the form you have provided.
Finally you can have a look at the full code example given.
In first case, both are same.
In second case, both are different.
console.log(dateArr1); // [ '2017-11-10', '2017-11-10' ];
console.log(dateArr2); // [ '2018-05-15', '2018-05-14' ];
So it's clear our UTC conversion is working fine.
Please have a look at the below code example and try to understand. It is simple.
Note» All the comments inside function are for inputDateStr: "Fri Nov 10 05:45:36 +0000 2017"
// All the comments inside function are for
// inputDateStr: "Fri Nov 10 05:45:36 +0000 2017"
function getMyUTCDate(inputDateStr)
{
var inputDateString = inputDateStr;
var date = new Date(inputDateString);
var dateArr = []; // To store 2 dates, simple one & full UTC one
console.log(date); // 2017-11-10T05:45:36.000Z
// ............... SIMPLE EXAMPLE ..........................
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes()
var seconds = date.getSeconds();
console.log(year); // 2017
console.log(month); // 10
console.log(day); // 10
console.log(hours); // 11
console.log(minutes); // 15
console.log(seconds); // 36
utcDate = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
utcDateString = utcDate.toUTCString();
console.log(utcDate); // 2017-11-10T11:15:36.000Z
console.log(utcDateString); // Fri, 10 Nov 2017 11:15:36 GMT
var dt1 = utcDate.toISOString().split('T')[0]
dateArr.push(dt1)
console.log(dt1); // 2017-11-10
// .................. FULL UTC EXAMPLE ..........................
var utcYear = date.getUTCFullYear();
var utcMonth = date.getUTCMonth();
var utcDay = date.getUTCDate();
var utcHours = date.getUTCHours();
var utcMinutes = date.getUTCMinutes()
var utcSeconds = date.getUTCSeconds();
console.log(utcYear); // 2017
console.log(utcMonth); // 10
console.log(utcDay); // 10
console.log(utcHours); // 5
console.log(utcMinutes);// 45
console.log(utcSeconds);// 36
var utcDate2 = new Date(Date.UTC(utcYear, utcMonth, utcDay, utcHours, utcMinutes, utcSeconds));
var utcDateString2 = utcDate2.toUTCString();
console.log(utcDate2); // 2017-11-10T05:45:36.000Z
console.log(utcDateString2); // Fri, 10 Nov 2017 05:45:36 GMT
// Get UTC Date
var dt2 = utcDate2.toISOString().split('T')[0]
dateArr.push(dt2)
console.log(dt2); // 2017-11-10
return dateArr;
}
// Inputs
var inputDateString1 = "Fri Nov 10 05:45:36 +0000 2017";
var inputDateString2 = "Mon May 14 23:59:36 +0000 2018";
var dateArr1 = getMyUTCDate(inputDateString1);
var dateArr2 = getMyUTCDate(inputDateString2);
// Print dates
console.log(dateArr1); // [ '2017-11-10', '2017-11-10' ]
console.log(dateArr2); // [ '2018-05-15', '2018-05-14' ]
References
developer.mozilla.org - Date processing functions
Thanks.
By default document.getTime() returns the number of milliseconds since January 1st, 1970, but if I want it from a different date do I have to use addition/subtraction to do so, or can I just pass some sort of parameter that accomplishes the same thing?
If you wanted a time from lets say December 25 2013
var date = new Date("December 25, 2013 00:00:00");
document.write( dt.getTime() );
You can't do that without subtraction, but this works:
var d=new Date("November 25, 2014 02:01:00");
var n=new Date("November 25, 2014 02:00:00");
var x = d.getTime() - n.getTime();
document.write(x + " milliseconds since "+n);
Please note, document.write shouldn't be used in production
If you want a time from now to anytime in the past, this will do it:
var d=new Date();
var n=new Date("November 25, 2014 02:00:00");
var x = d.getTime() - n.getTime();
document.write(x + " milliseconds since "+n);
I am trying to create a date object that is 90 days from a specified date for use in a date comparison, but am not getting the correct dates. I have isolated my problem to the code shown below.
var now = new Date();
var beforeNow = new Date();
var afterNow = new Date();
var myDate = new Date();
var beforeMyDate = new Date();
var afterMyDate = new Date();
beforeNow.setDate(now.getDate() - 90);
afterNow.setDate(now.getDate() + 90);
myDate.setFullYear(2011, 10, 22); // set to Nov 22, 2011
beforeMyDate.setDate(myDate.getDate() - 90); // set to 90 days before Nov 22, 2011
afterMyDate.setDate(myDate.getDate() + 90); // set to 90 days after Nov 22, 2011
The above is resulting in:
beforeMyDate: Fri Dec 23 2011 08:46:18 GMT-0800 (Pacific Standard Time)
myDate: Tue Nov 22 2011 08:46:18 GMT-0800 (Pacific Standard Time)
afterMyDate: Wed Jun 20 2012 08:46:18 GMT-0700 (Pacific Daylight Time)
Here's a fiddle: http://jsfiddle.net/LnmpR/9/
Any help would be appreciated. Thanks.
You have to pass the same base time to the beforeMyDate and afterMyDate Date instances. Currently, you're adding/removing 90 days of the current date (new Date() without parameters returns the current time).
Demo: http://jsfiddle.net/LnmpR/12/
var now = new Date();
var beforeNow = new Date();
var afterNow = new Date();
var myDate = new Date();
var beforeMyDate;
var afterMyDate;
beforeNow.setDate(now.getDate() - 90);
afterNow.setDate(now.getDate() + 90);
myDate.setFullYear(2011, 10, 22); // set to Nov 22, 2011
beforeMyDate = new Date(myDate); // Set base to myDate
afterMyDate = new Date(myDate); // Set base to myDate
beforeMyDate.setDate(myDate.getDate() - 90); // set to 90 days before Nov 22, 2011
afterMyDate.setDate(myDate.getDate() + 90); // set to 90 days after Nov 22, 2011
The values of beforeNow and afterNow might also be off by a few milliseconds, because all of them are new, parameterless instances of the Date object.
function formatDate (input) {
var datePart = input.match(/\d+/g),
year = datePart[0].substring(2), // get only two digits
month = datePart[1], day = datePart[2];
document.write(new Date(day+'/'+month+'/'+year));
}
formatDate ('2010/01/18');
When i print this i get Thu Jun 01 1911 00:00:00 GMT+0530 (India Standard Time) but the system is actually 3:42 P.M
Use the current date to retrieve the time and include that in the new date. For example:
var now = new Date,
timenow = [now.getHours(),now.getMinutes(),now.getSeconds()].join(':'),
dat = new Date('2011/11/30 '+timenow);
you must give the time:
//Fri Nov 11 2011 00:00:00 GMT+0800 (中国标准时间)
alert(new Date("11/11/11"));
//Fri Nov 11 2011 23:23:00 GMT+0800 (中国标准时间)
alert(new Date("11/11/11 23:23"));
What do you want? Just the time? Or do you want to define a format? Cu's the code expects this format for date: dd/mm/yyyy, changed this to yyyy/mm/dd
Try this:
function formatDate (input) {
var datePart = input.match(/\d+/g),
year = datePart[0],
month = datePart[1], day = datePart[2],
now = new Date;
document.write(new Date(year+'/'+month+'/'+day+" " + now.getHours() +':'+now.getMinutes() +':'+now.getSeconds()));
}
formatDate ('2010/01/18')
Output:
Mon Jan 18 2010 11:26:21 GMT+0100
Passing a string to the Date constructor is unnecessarily complicated. Just pass the values in as follows:
new Date(parseInt(year, 10), parseInt(month, 10), parseInt(day, 10))
You're creating a Date() object with no time specified, so it's coming out as midnight. if you want to add the current date and time, create a new Date with no arguments and borrow the time from it:
var now = new Date();
var myDate = new Date(parseInt(year, 10), parseInt(month, 10), parseInt(day, 10),
now.getHours(), now.getMinutes(), now.getSeconds())
No need to strip the last two characters off the year. "2010" is a perfectly good year.
I'm trying to write a javascript function that calculates the time since Oct 11th, 1910 so I can throw it into a timer for a project I'm working on. I get that javascript's milliseconds works from epoc, but I don't and can't find a way to get the milliseconds since a date earlier than 01.01.1970
Does anyone have any loose code that can do the above that they may be willing to share?
var oldGoodTimes = new Date(1910, 9, 11); // January = 0
var actualDate = new Date();
console.log(actualDate.getTime() - oldGoodTimes.getTime());
Try this:
var yeOldeTimes = new Date();
yeOldeTimes.setFullYear(1910, 9, 11);
var myNewDate = new Date();
console.log("Milliseconds since Ye Olde Times: " + (myNewDate - yeOldeTimes));
Number of milliseconds since Oct 11th, 1910
console.log(new Date() - new Date('1910', '10', '11'))
// new Date().valueOf() - milliseconds since 1970
// -(new Date('1910', '10', '11')).valueOf() - milliseconds from 1910 since 1970