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.
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
Im trying to get the date, but im wanting to to return ex ”Oct 29”. Ive tryed using split, but it changes everyday. Heres what I have so far.
Let date = Date().split(“ 2021”)[0];
This returns day<string> month day<int>
You can use the toLocaleString function to achieve what you want. This code below will generate the output you wanted ("Oct 29").
let date = new Date().toLocaleString('en-US', { month: 'short', day: 'numeric' });
You can format it there are various inbuilt methods or do this:
Let date = Date().toString().substring(4,10);
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 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());
This question already has answers here:
Convert dd-mm-yyyy string to date
(15 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 4 years ago.
I have this
let d = new Date("03-08-2018"); //dd-mm-yyyy
console.log(d.getMonth()); // returns 02, I want 07
I have my date in dd-mm-yy. The getMonth() thinks I'm using mm-dd-yy.
How do I get correct month and date.
Your date format is not standard, and I strongly recommend not to use such code on a client web code, because how it behaves would be client-dependent.
EDIT (thx RobG) : Don't use the builtin parser new Date(), this will get you into trouble depending on the client timezone, even if you use a proper format where the month seems to be correctly recognized.
You'll have to create some function to parse the string yourself and create a Date object with year, month and day manually set, that would be the safe way.
Example of manual parsing :
let regex = /^(\d{2})-(\d{2})-(\d{4})$/; // each parsing group is day-month-year
let date = '03-05-2018';
let match = regex.exec(date);
let month = +match[2] - 1; // starting at 0, remove -1 if you want 1-based month number
console.log(month); // returns 4 in this example '03-05-2018'
(of course you should also put some guards if the string is not matching the correct format)
JS Date give the month starts with 0. So Try this below for getting month from date
let d = new Date("03-05-2018"); //dd-mm-yyyy
console.log(d.getMonth()+1);
if you want 4, then you should add +2 ( but i am not sure why you want 4)
let d = new Date("03-05-2018"); //dd-mm-yyyy
console.log(d.getMonth()+2);
This question already has answers here:
JavaScript - Parse UTC Date
(4 answers)
How to show a UTC time as local time in a webpage?
(5 answers)
Working with timezones and daylight savings time in Javascript
(3 answers)
How to convert time correctly across timezones?
(3 answers)
Closed 4 years ago.
I have a string:
var utc_datetime = "02/09/2018 1:27 a.m."
I need it to show up in the user's local time. The format will always be the same, and it will always be queried in UTC timezone.
How can I accomplish this? I tried moment, and moment-timezone, but no luck so far.
Remove the dots and capitalize the AM or PM: Append 'UTC' to the string then convert it to a date
var utc_datetime = "02/09/2018 1:27 a.m."
var new_datestring = utc_datetime.replace(/\./g,'').toUpperCase() + ' UTC';
var date = new Date(new_datestring);
var local_datetime = date.toString();
console.log(local_datetime)