This question already has answers here:
How do I get the current date in JavaScript?
(61 answers)
Closed 8 years ago.
What I need to do is get the current date and do a math formula to figure out how many days from a recorded date in a record. Is there a Jquery or javascript function to do this?
you can use this code to find number of days between two dates.
var d1=new Date(2014,06,15);
var d2=new Date(2014,06,10);
var timeInSec=d1-d2;
var days=timeInSec/(1000*60*60*24);
Related
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed last year.
Hey guys I hope that you can help.
I have a Date string formated like '10/02/22 21:59'
Trying to find the time difference between NOW and some time in the future(string).
Tried the code below, but the result is not accurate, some 234 days off when the time difference should be juts 5 hours...
What am I doing wrong?
console.log( (Date.parse('10/02/22 20:59') - Date.now()) / (1000*3600*24) )
234.12668752314815
This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?
I was hoping it could be as simple as below, but it isn't quite that simple.
var givenDay = new Date(01/01/2020);
var hundredthDay = new Date(givenDay + 100);
console.log(hundredthDay)
You can try using .setDate() and .getDate() combination.
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
The getDate() method returns the day of the month for the specified date according to local time.
Adding the required days to .getDate() as the following:
const givenDay = new Date('01/01/2020');
console.log(givenDay);
const result = new Date(givenDay.setDate(givenDay.getDate() + 1 + 100));
console.log(result);
I hope this helps!
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Format JavaScript date as yyyy-mm-dd
(50 answers)
Javascript format date / time [duplicate]
(5 answers)
Get String in YYYYMMDD format from JS date object?
(53 answers)
Closed 5 years ago.
In javascript I need to change the current date in the following format?I googled a lot but I didnt find the exact result.
I'm expecting the current date to format is 22-Sep-2017 13:37:02
Thanks in Advance
var ele = document.getElementsByTagName('h3')[0];
setInterval(function(){
var date =new moment().format('DD-MMM-YYYY hh:mm:ss');
ele.innerHTML = date;}
,1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<h3></h3>
Use momentjs and format your date according to your need.
This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 7 years ago.
i have a date 2015-12-22 and i have a field to input number of days EX: 5 and i want to show result in other textbox 2015-12-22 + 5. Please give me any solution
you can use moment.js ,it is a very complete library, with several interesting features,Parse, validate, manipulate, and display dates in javascript.
This question already has answers here:
extract time from datetime using javascript
(10 answers)
Closed 8 years ago.
Is it possible to get the time from the following datetime in javascript?
2014-11-24 08:30:00
Any help would be appreciated.
UPDATE
Found the solution to my problem in the duplicate:
datetime.substr(11, 5);
//returns 08:30
Thanks for all your help!
What about slice? It should be the fastest when you know you always get datetime (and time is in last 8 chars).
var datetime = "2014-11-24 08:30:00";
var time = datetime.slice(-8);
alert(time); // returns "08:30:00"
new Date("2014-11-24 08:30:00").getTime();
This code block will get time from your exact date.