In javascript I have some datetime like this
Date: '2017-07-04'
I want to convert it to DateTime like ajax get result.
Expect result like this:
'/Date(1565089870830)/'
How can I make it possible?
You can use Date.parse(). This method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
var date = Date.parse('2017-07-04');
console.log(date);
The format you're trying to create is a string containing an Epoch timestamp. To create that in JS you can create a Date object from the input string and retrieve the getTime() property. Then it's just a matter of concatenating that value in to the format needed. Try this:
var date = new Date('2017-07-04');
var epoch = date.getTime();
var output = `/Date(${epoch})/`;
console.log(output);
Presumably you're working with an ASP.Net MVC site, given the date format you're trying to build. One thing to note here is that you don't need to use that format when sending DateTime values back to the server. You can send any string so long as it can be bound to a DateTime instance by the ModelBinder. As such I'd recommend using an ISO8601 format instead.
Related
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
I need to format this value in milliseconds "1543325996" to date like this "18/01/1970, 11:42:05 PM". I've already got the right result using 'toLocaleTimeString' function, but this result has String type. I need exactly Date type.
function dateFormat(date) {
var formDate = new Date(+date).toLocaleDateString("en-GB");
var formTime = new Date(+date).toLocaleTimeString("en-US");
var concatDate = (formDate + ", " + formTime);
// here I've got error 'Invalid Date'. I know that it's a wrong way, but don't know what to do.
var newDate = new Date(concatDate);
return newDate;
}
but this returns error "Invalid Date". Is there another way to convert String to Date?
...but this result has String type. I need exactly Date type.
Date objects don't have a format. Formatting is intrinsically a textual thing (e.g., string).
If you want Dates, then new Date(+date) is giving you that. There's nothing further required. Later, at some point, if you want to display that date in a textual form, use toLocaleDateString or Intl.DateTimeFormat or similar to format them in the way you want them formatted. But not until/unless you need to convert them to text (a string).
I have a date sting that looks like this 2016-02-21T02:14:39.000000
would like to convert it to Epoch time using Javascript if possible
Try
var ts = "2016-02-21T02:14:39.000000";
var unix_seconds = ((new Date(ts)).getTime()) /1000;
console.log(unix_seconds);
getTime returns milliseconds, so divide by 1000 to get seconds
https://jsfiddle.net/tbxac0de/
Presumably by "convert it to Epoch time" you mean a number of seconds or milliseconds since the common UNIX and ECMAScript epoch. The time value can be found by converting the string to a Date and getting its internal time value.
By far the best way to convert a string to a Date is to manually parse it. A library can help, but a function isn't difficult to write. E.g. to parse "2016-02-21T02:14:39.000000" as a local date (i.e. ISO 8601 format without a time zone), use something like:
// Parse y-m-dTh:m:s as local date and time
// since there is no timezone
function parseIsoLocal(s) {
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5],
((b[6]||'')+'000').slice(0,3));
}
// Convert string to Date
var d = parseIsoLocal('2016-02-21T02:14:39.000000');
// Show date and milliseconds since epoch
document.write(d + '<br>' + +d);
The above can easily be extended to treat the string as UTC, incorporate time zones and validate the input, but that doesn't seem to be required in this case.
Note that most browsers will parse the format in the OP, however some in use will not and, of those that will, some treat it as local and some as UTC. According to ISO 8601, it should be treated as local so that's what I've done.
there is a situation which I need to convert a date string which consist a timezone abbrevation to a Moment object and parse it. How can I do this? sample date is like below:
var dateString = "2015-01-14 06:57:47 ECT";
be aware that I need the timezone of this date, because Im going to do another conversion to another zone, so if we just consider the date we miss the accuracy.
I don't think moment can handle a timezone name, you should replace ECT with the offset value, like +0200
I have a date value like this value.game_date = 2013-10-27 03:39:35 and I'm trying to parse it as follow:
moment().format(value.game_date, 'DD-MM-YYYY');
But I get this as result 29-10-2013 00:00:00 where I'm looking for this format: 29-10-10-29 without hour, what I'm doing wrong?
I think you have some typos or misinformation in your post regarding the output you're getting and what you're desiring.
Based on what appears to be a misunderstanding of moment, however, I am pretty sure that what you want is:
value.game_date = '2013-10-27 03:39:35';
var formatted_game_date = moment(value.game_date).format('DD-MM-YYYY');
// produces '27-10-2013'
moment() is a factory function which takes a date string and returns a moment instance. That moment instance then has various methods available, such as format() which takes a format string as the first param.
So your code is producing a moment instance representing current date/time (because you're not passing any params to moment()), then you ask .format() to return a string formatted using your date stamp as the formatter. Your date string doesn't have any of the things in it which format would parse and replace, so you just get back your date string.
The code I gave passes the date string to moment to produce the instance, then asks .format() for a formatted string using your desired format template.