This question already has answers here:
Display date/time in user's locale format and time offset
(17 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
i want get current time, when i convert new Date() into sting but when it converts reduces 5.30 hours based on my time zone.
How can i get same time which returns in new Date() method?
Try this
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
How ever format you want you may adjust
Related
This question already has answers here:
How do I get a human readable date from a unix timestamp in Javascript?
(2 answers)
Closed 2 years ago.
I've been trying to convert a UNIX timestamp into a YYYY-MM-DD HH-MM-SS date for a Discord Bot, but to no Avail, is ther any way I could do this properly (Since I've been comparing results with a webpage)
Parsing a UNIX timestamp is as easy as just assigning a number to the Date constructor.
let unix_timestamp = Date.now();
var date = new Date(unix_timestamp);
console.log(date);
Date.now() retrieves the current UNIX timestamp
On how to format a date, I believe this question will help.
You can use moment.js.
moment.unix(value).calendar();
Found an answer thanks those who contributed
var d = new Date(convertIDtoUnix(ID));
timeStampCon = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear() + " " + d.getHours() + ':' + d.getMinutes();
This question already has answers here:
Add A Year To Today's Date
(9 answers)
Closed 3 years ago.
I want to get the timestamp of one-year from now:
var oneYr = new Date();
oneYr.setYear((new Date()).getYear() + 1);
When I try to get the timestamp:
oneYr.getTime() /1000
I get -58351759111000 (not correct)
It's only working for date in the past.
Any idea how to get timestamp for future date?
Use getFullYear instead of getYear.
var oneYr = new Date();
oneYr.setYear((new Date()).getFullYear() + 1);
This question already has answers here:
Get the time difference between two datetimes
(22 answers)
Closed 5 years ago.
I am trying to get the day/hr/min difference from two JavaScript datetime stamps, the current time subtract a future event time which will then be displayed on the application front end.
two dates:
Future event date - 2017-10-01 18:00:00
Current date - now()
Current code:
var currentTime = new Date();
var eventStarts = results[0][i].eventstarts;
var difference = differenceInMilliseconds(eventStarts, currentTime);
var date = new Date(difference);
var days = date.getDay();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var formattedTime = days + ':' + hours + ':' + minutes.substr(-2);
This isn't correctly working and i cant figure out why, its returning a 2 day difference which obviously isn't correct, it should be returning upwards of 29 days.
I'm currently using the npm package date-fns and i don't mind trying out another package if that will help out
When you have a Date, it's based on the 1st january 1970. So when you are setting up new Date(difference). You are setting a date related to 1st january 1970 - so not what you are looking for.
I would recommand you to use the library moment.js, that allow easy date manipulation.
#manzurul example Here
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
console.log(d.days(), d.hours(), d.minutes(), d.seconds());
This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 5 years ago.
Given inputs
1) Datetime : day month year hh and mins
2) Timezone
How to create a Javascript date object of a specific time zone or date in UTC ?
Also how to convert this date time to any other timezone.
Is there any package in angular that can do this.
So I do it like this:
var offset = -5.0; // my servers UTC offset
var clientDate = new Date();
var utc = clientDate.getTime() + (clientDate.getTimezoneOffset() * 60000);
var serverDate = new Date(utc + (3600000 * offset));
var c = $.datepicker.formatDate('mm/dd/yy', serverDate);
In the last line, I use the jQuery (commonly called BootStrap) datepicker to format the date for me.
The key here is offset which is the hours offset from UTC time.
In Angular you can use dateFilter
new Date(dateFilter(minDate, 'yyyy-MM-dd\'T\'00:00:00', timezone));
This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?
var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);
Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);