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.
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:
javascript: how to parse a date string
(4 answers)
Closed 5 years ago.
I want to convert 06/29/2017 10:13AM to a Javascript Date instance. Currently I split the time into 06/29/2017 and 10:13AM and converted 06/29/2017 using Date('06/29/2017').getTime(). I want to convert the time using the same way and adding the two together, but it isn't working. How should I go about doing this and is there a better way to do it?
Assuming you simply want to convert it a Date-object, try using an UTC date:
new Date('2017-06-29T10:13:00+00:00'); // change to desired time zone
This question already has answers here:
Problem with date formats in JavaScript with different browsers
(4 answers)
Closed 7 years ago.
new Date().toLocaleString() --> "24/09/2015 10:14:00 PM"
new Date("2015-09-24 09:38:32.639").toLocaleString() --> "Invalid Date"
How can I format a date object from a timestamp in string format?
SOLUTION: At the end I got it fixed changing my date type in the server from DateTime to Instant, js will atomatically add zone offset automatically from a timestamp and will format the dates in the right way.
NOTE: I know this question is duplicated, however the solution proposed is different and may help other users to get a different approach to their code.
var myDate = "2015-09-24 09:38:32.639";
new Date(myDate.replace(/-/g,"/")).toLocaleString()
Now it's working fine
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);