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());
Related
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
When the date is passed from my c# to JavaScript it returns the date time as {4/3/2020 12:00:00 AM}
but in JavaScript it is shown as 1585852200000.
What is the format that is being used? And how can i convert it back?
You need to convert the Unix timestamp to DateTime format,
var localDate = new Date(1585852200000).toLocaleDateString("en-US")
console.log(localDate); // only local date
var localTime = new Date(1585852200000).toLocaleTimeString("en-US")
console.log(localTime) // only local time
// local datetime
console.log(new Date(1585852200000).toLocaleString());
1585852200000 is epoch date.
you can convert it as
var date = new Date(1585852200000)
console.log(new Date(1585852200000));
As an alternative from Shivaji's answer:
When you are passing the date through to JS you could cast it as a string with DateTime.ToString("dd/MM/yyyy") seen here on MSDN.
This will keep its integrity visually, if it is just for display purposes, otherwise you will need to re-cast appropriately in JS (in which case use Shivaji's answer).
JavaScript Date's object will return the DATE object and it's POSITION that is being assigned in your computer. So, when you are working with a date or datetime types, you can use some of the methods that are provided by the Date object, such as getDate() and getDay(). But, a better solution would be to format the Date object itself. For example: use the toString() or toUTCString() methods.
var d = new Date();
document.getElementById("demo").innerHTML = d.toString();
Reference:
https://www.w3schools.com/js/js_date_formats.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Is there any way to convert any date string (not necessarily current date) (could be any format) to specific date format in Javascript. Like converting "MM-DD-YYYY" or "ddMMYYYY" to "DD-MMM-YYYY"?
I know that from current date as var date = new Date(), we can get time and hours but what to do in case of existing date string like "31/01/1999" to "31-JAN-1999".
Given the input date string can be of any format.
This is a common problem.
You should be able to do it with moment.js.
Ex.
moment("31/01/1999").formatWithJDF("dd - MM - yyyy");
Have a look here, https://momentjs.com/docs/ for more details.
Using DateFormatter.js
var date = new Date('2020-03-25 10:30:25');
var formatter = new DateFormatter();
displayFormat = 'D M d Y h:i:s';
var dateString = formatter.formatDate(date, displayFormat); // Wed Mar 25 2020 10:30:25
This can be done by first checking the input date format with the arrays or Map of regex provided, then need to convert that format to JS accepted format ISO format.
Once converted to ISO format this date now can be converted to any form with logic written for it in separate functions.
I need to convert a date to "Date.now()" like format in javascript. For eg. using Date.now(), I'm getting the value as 1395150601449. I need my date to be formatted like this. How can I achieve this ? I would like to create a custom filter in angularjs for this formatting.
Any help will be highly appreciated.
Call the .getTime() function.
var d = new Date();
console.log(d.getTime());
You can do the opposite by passing such a timestamp to the Date constructor. Thus, to make a copy of a date instance:
var copy = new Date( d.getTime() );
From a weather feed I'm getting the data for dates in the form of 2012-05-17.
How do I convert that to Thursday May 17th form?
Parse the string into a JavaScript Date object.
Format the Date object however you like.
See also: Why does Date.parse give incorrect results? and How to format a JavaScript date questions.
How about this:
var originalDateTime = '2012-05-17',
splitedDatetime = originalDateTime.split('0'),
date = new Date(),
date.setFullYear(splitedDatetime[0],splitedDatetime[1]-1,splitedDatetime[2]),
formatedDateString = date.toLocaleFormat("%A %B %d");