How to add hours in datetime in JavaScript [duplicate] - javascript

This question already has answers here:
How to add hours to a Date object?
(20 answers)
Closed 4 years ago.
I want to add hours in DateTime. I have googled it but couldn't find any code.
Here is this datetime 2018-07-25 20:23:22. I want to add hours in this datetime so it gives me new datetime in the same format
I have tried this.
var datetime = "2018-07-25 20:23:22";
datetime.setHours(datetime.getHours()+5);
But It didn't work.

Your datetime is String. You need to convert it into Date object first.
var datetime = new Date("2018-07-25 20:23:22");
console.log("Before: ", datetime);
datetime.setHours(datetime.getHours()+1);
console.log("After: ", datetime);
I would prefer to use moment.js library for manipulating and playing with dates.
Hope this may help you.

Here is the simple way with using Moment.js
const date = moment('2018-07-25 20:23:22');
date.add(5, 'h');
console.log(date.toString());

Related

Timestamp to date format and reverse [duplicate]

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 1 year ago.
Hello Guys!
I just wrote some code with JavaScript and Firebase, I obtained the date from Firebase in milliseconds and converted it to date format with this code.
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toLocaleString("en-US", {day: "numeric"}) + '/' + dateObject.toLocaleString("en-US", {month: "numeric"}) + '/' + dateObject.toLocaleString("en-US", {year: "numeric"});
Now I cant find the way back with this kind of date format I used. I want to edit the product but when I try to convert this date I canĀ“t find a useful method.
Maybe should I use LocalStorage to storage the date in milliseconds then search for it and use it?
Thanks anticipated!
Regards, Gaspar.

how to get javascript Current Date [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
How can I get current date and hour like this 2020-07-06T12:30:00.
in JavaScript / react
can you help me?
So you can use builtin Date object in JS.
const today = new Date()
let day = today.getDay()
let month = today.getMonth()
let year = today.getFullYear()
console.log(`${year}-${day}-${month}`)
You can use new Date() to access the built in JS date, and to access it the way you are specifying you want the new Date().toJSON() which will give you "2020-07-06T20:25:23.671Z" for example.
You can use builtin Date object.
new Date();
You can use toJSON like
new Date().toJSON()

JS formatting date ymd to yyyy-mm-dd [duplicate]

This question already has answers here:
Moment.js - How to convert date string into date?
(3 answers)
Closed 3 years ago.
I am getting my date from PHP in format ymd. I am using Moment.js to convert it to format I need, but it's not working properly.
So for example today's date is comes like 190528 but after conversion to YYYY-MM-DD it becomes 190528-1-1 instead of 2019-05-28.
Is it the first format of the date which causes the problem, or there is a way to overcome this and convert it the way I need?
Parse Date first by passing format as 'YYMMDD'
moment("190528", "YYMMDD").format("YYYY-MM-DD");
This will give proper output.
You do not need to load moment for this.
You can break the string into parts of years, month and date. Format the year and return the joined string...
function formatDate(date) {
var _d = date.match(/.{1,2}/g);
_d[0] = "20" + _d[0];
return _d.join("-");
}
var d = "190528";
console.log(formatDate(d));
You need to tell moment what format your date string is in:
console.log(moment("190528", "YYMMDD"))
<script src="https://unpkg.com/moment#2.24.0/moment.js"></script>
You can pass the format of current date as a second param of moment.
console.log(moment("190528", "YYMMDD").format("YYYY-MM-DD"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Here the docs.

Javascript convert Datetime to Time [duplicate]

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.

How should I instantiate this date in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Format Date in Javascript
I have this date:
var newDate = "/Date(1333609200000)/";
And what I need to do to it is:
var myDate = new Date(1333609200000);
I thought about just trimming off "/Date()/" and then getting just the number inside but I wasn't sure if that would be best here. Should I just simply regex the number out of that? Is there a better approach?
Notes: The date is being created by c#'s JavascriptSerializer and comes in the format newDate above. It cannot be changed in c# due to constraints.
You could do it like this:
var newDate = "/Date(1333609200000)/";
newDate= new Date(parseInt(newDate.match(/\d/g).join("")));

Categories