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.
Related
I have a timestamp with this format 09:53:56,07-04-2021
How do I convert it to ISO format ?
I have tried
moment("09:53:56,07-04-2021").format("hh:mm:ss,mm-dd-yyyy")
I'm getting "Invalid date" message
There are two separate function calls, always executed consequently, in your code. The first of those...
moment("09:53:56,07-04-2021")
... is parsing, when Moment constructor tries to create an object from the string parameter it gets. The next call is formatting:
.format("hh:mm:ss,mm-dd-yyyy")
... when Moment uses the string argument to prepare and return a proper representation of its internal date object.
The key is that the first call cannot use the data of the second one. That's why it actually doesn't matter which format you pass there. By the time it's called Moment already misinterpreted the string - and created a messed up date object out of it.
What you need is supplying the correct format into Moment constructor as its second param, like described in the docs. There are two options:
moment('09:53:56,07-04-2021', 'HH:mm:ss,MM-DD-YYYY')
moment.utc('09:53:56,07-04-2021', 'HH:mm:ss,MM-DD-YYYY')
The last one should be used if the datetime provided is always in GMT.
Note that mm in format string means "minutes"; you need M or MM (uppercase) to refer to the "month number" instead. In this case, it's MM, as it's clear that two digits are used.
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.
I am getting in following format from PHP:
"end_date":{"date":"2017-02-17 18:52:31.000000","timezone_type":3,"timezone":"UTC"}}]
at JS/AngularJS end I am doing following:
var end_date = Lease.period_ferme[idx].end_date
$scope.frame[idx].end_date = moment(end_date).toDate()
console.log('After');
console.log($scope.frame[idx].end_date); //invalid date
if you pass an Object to the Moment constructor, that object should have fields named year, month, etc. Your object does not, so Moment can't parse it and decides it's invalid.
As mentioned in the comment attached to your question, try creating a Moment object with just the date string:
$scope.frame[idx].end_date = moment(end_date.date).toDate();
or, since your JSON specifies UTC, try creating a Moment object using Moment.utc:
$scope.frame[idx].end_date = moment.utc(end_date.date).toDate();
I am querying data using OData, url looks like http://myurl.com/api/Customer?$filter=ResDate eq DateTime'2014-03-15T12:01:55.123'.
I'm getting date/time from jquery.datepicker instead of the static date and using moment.js to convert from DD-MM-YYYY to YYYY-MM-DDTHH:mm:ss in order to pass it to web service.
function convertDateToISOdate(date){
var newDate = moment(date,'DD-MM-YYYY').format('YYYY-MM-DDTHH:mm:ss');
return newDate;
}
Date returns from the function, is 2014-03-15T00:00:00.
Problem : 2014-03-15T12:01:55.123 is not equal to 2014-03-15T00:00:00, so there's no record selected.
What I want is , just to compare the date , not include time stamp.
Note : I can not change the format date/time at server side(Web service) because it's not belongs to me.
Any idea is much appreciated.
Your first call to moment(date,'DD-M-YYYY') is stripping the time information from the incoming data. try using moment(date) (no format) instead because momentjs recognizes your incoming date format intrinsically, without having to be told which format to use, and will correctly parse the H:M:S data, too.
MomentJS date parse information
I am calling my database which contains a datetime datatype. The date looks like this:
2005-05-23 16:06:00.000
I would like to display this in a table when a user selects a certain item from a list. I call my controller action and return Json of all the times and put them in a table. The problem is the date is completely wrong. What is displayed is this:
/Date(1255470180000)/
The date that is returned isn't even parsable (which I don't want to do anyway) so I can't even get the data if I wanted to. Any ideas?
The date you're getting back is serialized to a marker and a number of milliseconds since midnight 1st Jan 1970 (in UTC). If you isolate the numeric portion, convert it into a number, and feed it into the Date constructor you'll get an actual date to work with, which you can then format as you like.
var ticks, dt;
// Isolate the numeric portion of the value
ticks = /[0-9]+/.exec(json.dateValue)[0];
// Convert to a number
ticks = parseInt(ticks);
// Convert to a date
dt = new Date(ticks);
Alternately, if the JSON serializer on the server supports a "replacer" parameter as Crockford's and ECMAScript 5th edition's do, you could supply a replacer that formatted the date into a string server-side and handle it there, since you said you don't want to parse the date client-side (although the jQuery tag suggested to me maybe you did).
The other alternative is to return the formatted string from the controller action. You could even leave the timestamp and return a second field as "Formatted Timestamp" or something similar.
var listFromDb = ...
return new Json(listFromDb.Select(itemFromDb => new List { new
{ Date = itemFromDb.Date, FormattedDate = FormatDate(itemFromDb.Date), ...}
I ended up formatting the code in the controller action instead.
I just cast the datetime property to a string using .ToString() and got the desired results.
Thanks for the help though guys.