How to convert a UNIX timestamp to date in JavaScript [duplicate] - javascript

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();

Related

How to get exact time when converting date into string [duplicate]

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

How to get only date part (dd/mm/yyyy) of a date object without time in JavaScript? [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I am trying to create date object without time info like 23/09/2019.
I already tried many solutions like;
new Date(d.setHours(0,0,0,0))
new Date(dateString).toUTCString().split(' ').slice(0, 4).join(' ')
new Date(d.getFullYear(), d.getMonth(), d.getDate())
new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())
However, all of this either give me string or date with time Tue Aug 27 2019 00:00:00 GMT+0300
I want to get this 23/09/2019 as a date object. When I write typeof result it should return date.
I know js Date object stores date and time but
Is it possible to get something like this?
Have a look at Date - Javascript | MDN for Date methods to see more details. You can compose the date components however you like.
If you want the date/month/year, you just need to call those methods:
function zeroPad(n) {
return n < 10 ? '0'+n : n;
}
var d = new Date();
var s = zeroPad(d.getDate()) + '/' + zeroPad(d.getMonth()+1) + '/' + d.getFullYear();
console.log(s);
See also getUTCDate, getUTCMonth, getUTCFullYear for UTC.

Javascript date parse error? [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 5 years ago.
let date = new Date("2017-09-12T12:00:00");
console.log(date.getUTCMonth());
Here I am expecting it would log 09 for a month but it's logging 08.
Year, day , hour and minute gets parsed correctly though. what's going on here? How can I extract 09 from the above date string?
The getUTCMonth() is a zero-based value — zero is January.
For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth
date.getUTCMonth() method returns the month (from 0 to 11) for the specified date.
So to get what you expect, you should add 1.
Months are 0-indexed in Javascript.
var date = new Date(date),
month = '' + (date.getMonth() + 1),
day = '' + date.getDate(),
year = date.getFullYear();
For a quick example is how you would format it if you wanted to format it in a YYYY - MM - DD format.

Javascript...add one day to date and change the format [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I have this date here 2013/06/10, which comes from the database and is set in a variable called date.
I added one day to this date by doing this..
var endDate = new Date(date);
endDate.setDate(endDate.getDate() + 1);
and now I am trying to change the format to yyyy/MM/dd
var finalEndDate = endDate.toString('yyyy/MM/dd');
alert(finalEndDate);
but this returns
Tues Jun 11 2013 Eastern Standard Time, etc.
How do I fix this?
As far as I know, toString does not take any arguments. It's easy to construct your format though.
var finalEndDate = endDate.getFullYear() + '/' + (endDate.getMonth() + 1) + '/' + endDate.getDate();
There are several getter methods for each component of the date object to help you construct nearly any format.
I strongly encourage you to take a look at Moment.js
var str = moment(date, 'YYYY/MM/DD').add('days', 1).format('yyyy/MM/dd');
Note: moment doesn't know yyyy, what's that supposed to be? See http://momentjs.com/docs/#/displaying/format/ for supported format strings.

Current time in YouTube date API format

I am using the YouTube API and it returns uploaded & updated times in this format: 2013-05-13T13:12:42.000Z (ISO 8601). How can I get the relative time using Javascript (or jQuery)?
Some sources that I've tried to combine to get this to work, however they all appear to format the date differently.
Javascript time to relative
Format ISO-8601 into a date object
Use this timeDifference(new Date(), new Date().setTime(Date.parse("2013-05-13T13:12:42.000Z")))
If you have a problem with the "(ISO 8601)" part, use this timeDifference(new Date(), new Date().setTime(Date.parse("2013-05-13T13:12:42.000Z (ISO 8601)".replace(/ *\(.*\)/,""))))
Click here to check the Demo
var MyDate = new Date(
Date.parse
(
"2013-05-13T13:12:42.000Z (ISO 8601)"
.replace(/ *\(.*\)/,"")
)
);
var date_Str = MyDate.getMonth() +
1 +
"/" +
MyDate.getDate() +
"/" +
MyDate.getFullYear();
Add more function
1. MyDate.getHours()
2. MyDate.getMinutes()
3. MyDate.getSeconds()
Result - "5/13/2013"

Categories

Resources