Display time from string timestamp in javascript - javascript

I am trying to display the date from time stamp using JavaScript but not working please check my code and it's not working due to string time but if i passed in number then it's working but this time coming from API so i must need to do something here. can anyone please help me.
var timestamp = '1607110465663'
var date = new Date(timestamp);
console.log(date.getTime())

You can convert string to number using methods such as Number or parseInt before creating Date object.
e.g.
var timestamp = '1607110465663'
var date = new Date(Number(timestamp));
console.log(date.getTime())

Parse string into int then...:
var timestamp = '1607110465663'
var date = new Date(parseInt(timestamp));
and use date object
console.log(date)

Remove the '' from timestamp
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log(date.getTime())
console.log(date)
See JSfiddle

Related

How to add days to current date in angular

Using this:
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
I get a date and time like this 2018-12-30T14:15:08.226Z, but only i want is this 2018-12-30. How can I retrieve just the date?
**This is Fixed. Thank You everyone who helps!!!
You're experiencing a JS problem, it has nothing to do with Angular.
This will use Date methods to get all the data you want:
const date = new Date ();
let dateString = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
// 2018-12-26
You can take advantage of the fact that the ISO 8601 format (what you are getting by implicitely converting to string) is well-codified with a separator T between the date and time in order to split it.
toISOString() gives you what you're seeing. split("T") splits the string into an array of strings with T as separator. [0] then extracts the first element.
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
console.log(myDate.toISOString().split("T")[0]);

How to convert "date(1494343074100)" that into formatted datetime in javascript

I am passing a date time from controller to view. In view i can get the date as date(1494343074100). How can i convert the date into formatted datetime. please any one help
Below is a way to convert to date.
var time = 1494343074100;
var date = new Date(time);
console.log(date.toString());

Comparing date with date in string javascript

I am trying to compare a couple of dates in javascript. First of it, I got froma database. In database it is in date format but after send it in a json file I guess it is just a string.
The second I got it in Date format in javascript. I found this script in javascript:
var todayAEJ = new Date();
var ddAEJ = todayAEJ.getDate();
var mmAEJ = todayAEJ.getMonth()+1; //January is 0!
var yyyyAEJ = todayAEJ.getFullYear();
if(ddAEJ<10){ddAEJ='0'+ddAEJ} if(mmAEJ<10){mmAEJ='0'+mmAEJ} todayAEJ = ddAEJ+'-'+mmAEJ+'-'+yyyyAEJ;
ANd it works like a charm.
The other date is like this: 13-01-2014
I tried to compare like this:
if(todayAEJ > val.date_End)...
But it returns true when today's day is bigger than val.date_End's day. So I cannot use this form when the month is diferent. What can I do in this case?
otherDate = '13-01-2014';
myDate=myDate.split("-");
var newDate=myDate[1]+","+myDate[0]+","+myDate[2];
otherDateTimeStamp = new Date(newDate).getTime();
todayAEJTimeStamp = new Date().getTime();
if(todayAEJTimeStamp > otherDateTimeStammp){
// your code
}
you can even use var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
Use above code...it will solve your problem!
Iinjoy...
If you can use MM-DD-YYYY format instead of DD-MM-YYYY you can use Date.parse:
var todayAEJTimeStamp = Date.parse(todayAEJ);//Date parse needs `MM-DD-YYYY` format
//if val.date_End is instance of Date
if(todayAEJTimeStamp > val.date_End.getTime()) {
//your code here
}
You have to compare them via microtime
var date = new Date("11/21/1987 16:00:00");
var milliseconds = date.getTime(); // Compare this milliseconds
Apart from timestamp solutions, I would recommend to use date.js in any case you want to play with Date object in JS. Date object has pretty odd behaviour in JS, and this library comes in handy.

Convert INT to TIME using JS

I have converted a feed's published date "pubDate" to integer using JS.
Here is the code I used for it.
function timetoint(date){
var date = new Date(date);
return Math.round(date.getTime()/1000);
}
Now I need to convert the result of the above function back to a time format.
How can I do it?
Use the Date constructor :
var date = new Date(time*1000);

Formatting system date, and comparing error

I have a String called yourDate 2012-11-30. Now i need to compare this date with the system date to check if it has exceeded or equaled the system date.
How can i do this ?
My approach;
var tDay= Ext.Date.parse("2012-11-30", "Y-m-d");
var sDay= new Date().dateformat('Y-m-d'); // system date
I get an error message TypeError: (new Date).dateformat is not a function. How can i correct this ?
note: I am using Extjs
Try the following:
var sDay = Ext.Date.format(new Date(), 'Y-m-d')
But I don't think you need to format your current date for comparison.
You can just use var sDay = new Date() and then compare date objects like if(sDay > tDay)

Categories