简体   繁体   中英

How to convert into Date time

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM