Lets say I have the following time: 6:47 AM
or 2:40 PM
How would I transform it so JavaScript Date object so I could use it for comparing different times?
Do I need to use a library or something?
Any suggestions will be amazing!!
Lets say I have the following time: 6:47 AM
or 2:40 PM
How would I transform it so JavaScript Date object so I could use it for comparing different times?
Do I need to use a library or something?
Any suggestions will be amazing!!
sorry, malfunction in my brain, wrong canned message
While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.
With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).
It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.
Please provide some example of what you’ve tried and I’m sure you’ll get more help.
Happy coding
There’s a lot you can do with the Date
object. I suggest looking at the MDN documentation and seeing if it answers your question. Play around with it and if you can’t get it to work quite right, show us your code and tell us what your having trouble with.
@ArielLeslie @ilenia I think I wrote the question a bit quick and small.
Context
I am web scraping a page that gives me back strings of a time an event happens like 6:45 AM
. However, I want to convert the human-readable time to time that I can use in JavaScript to perform different methods on.
Don’t worry about me not understanding something, I am very good at JavaScript, but not good when it comes to working with the Date object.
I have searched for this on google and found not anywhere near my goal.
This is my code I am experimenting with:
let dateTo = '6:45 AM'
console.log('NORMAL: ' + dateTo);
// console.log('TESTING: ' + Date.parse(dateTo));
console.log('MOMENT: ' + Date.now(moment(dateTo).format()));
console.log(moment(1605809765214).format());
console.log('WELCOME DATE: ' + new Date(dateTo).toString());
All of them are solutions I am experimenting with.
BTW this is part of a personal project.
Assuming that you want a date that is today at that given time, here’s the first thing that comes to mind for me:
Date
with new Date()
(this will be the current day and time).setHours()
and .setMinutes()
to update your Date
object.If other data is giving you the actual date, then you can simply use new Date(year, month-1, day, hours, minutes)
I don’t need today’s date. I want to convert the human-readable
time to a JavaScript object.
I already have the time which is 6:45 AM
. How do I get the JavaScript object from the time? Like parse the time out.
the Date object is a date, you can’t remove the date part
you can make an object like
{
hh: 6,
mn: 45
}
This is what I am looking for:
I already have: 6:45 AM
.
I want result of: 1605875066076 // Date.now()
But… I need the Date object to contain the time I already have. Then I could run the Date
methods on it.
what date do you want linked to the time?
because Date.now()
gives unix time, which also includes the date, as it is the number of seconds from the beginning of January 1st 1970
this in date and time format is 11/20/2020 @ 12:34pm (UTC)
I want the current day + the provided time.
The time I provided was just an example what type of result.
it is was @ArielLeslie was saying
but, new Date
may be what you want to use:
https://www.w3schools.com/js/js_dates.asp
How would I pass in a custom time like: 6:45 AM
to new Date()
?
Never mind, the solution was already in the post you mentioned. Oof, how did I not see that!!
if you want today
let date = new Date(); // this capture this instant
date.setHours(...);
date.setMinutes(...);