I am using this piece of code to get string representing date yyyy-mm-dd from a hidden field and then format it as needed:
var date_string = $('#end-date').val();
var splitDate = date_string.split("-");
var end_date = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
end_date.format("dddd, mmmm dS, yyyy")
But it throws an error:
end_date.format is not a function
Why does it happen and how to solve this issue?
That is because .format is not a native JavaScript function on Date.prototype.
You need to add a lib like this one: http://jacwright.com/projects/javascript/date_format/
I personally use http://momentjs.com/ to manage dates in JavaScript
You get this error because Date.prototype.format just does not exist (I am wondering why you think it does).
See this question about solutions how to format dates.
Related
I'm trying to test an API to check if the dates are present in the comment. The date is given in ISO format in the comment like 2020-02-18 21:30:13
When I compare with the Date() and convert it to ISO format, the format is slightly different from my date which makes my test to fail. How do I make the format the same as the one is my API response?
Below is my code:
var dateobj = new Date();
var B = dateobj.toISOString();
pm.test("Comment has Date", function (){
pm.expect(responseBody.split("*/")[0]).to.include(B)
})
Something Like This?
Javascript is not very flexible with Dates. But I think creating a formatting function shouldn't be a problem at all, try this:
var date = new Date();
var formattedDate = (date)=>{
return (`${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);
}
alert(formattedDate(date));
I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.
any help would be appreciated.
This code may solve your problem.
moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');
DEMO at https://jsfiddle.net/nffswx75/
var dt = "07/2017";
alert(moment(dt,"MM/YYYYY").format('YYMM'));
alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));
alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));
You can let moment create a date object from a string by telling it what format your date is in.
let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");
How can i change the current date to this format(DD/MM/YYYY) using moment.js?
I have tried below code.
$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");
But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.
You need to call format() function to get the formatted value
$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")
The syntax you have used is used to parse a given string to date object by using the specified formate
You can use this
moment().format("DD/MM/YYYY");
However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
This worked for me
var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
This actually worked for me:
moment(mydate).format('L');
for anyone who's using react-moment:
simply use format prop to your needed format:
const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
A safe way to do this
moment.locale('en-US');
moment().format("L");
"06/23/2021"
moment.locale('fr');
moment().format("L");
"23/06/2021"
I have the following HTML:
<strong id="ut-open-date">27/06/2014</strong>
and I want to read the text/innerhtml and convert its format to "YYYY-MM-DD" so I can insert it into MySQL table. I am using the moment.js library and my code is below:
var CreateDate = moment(jQuery('#ut-open-date').html()).format("DD/MM/YYYY");
CreateDate = moment(CreateDate).format("YYYY-MM-DD");
But the code changes 27/06/2014 to 2016-06-03 and I cannot work out why.
I also tried this code with the same result.
var CreateDate = moment(jQuery('#ut-open-date').html()).format("YYYY-MM-DD");
Any help is appreciated.
If we break down your code step by step you can see where it is going wrong:
var CreateDate = moment(jQuery('#ut-open-date').html())
This part uses the default constructor to try to parse the date, this is unreliable at best and has been deprecated. So moment is trying to guess what the date format is here.
.format("DD/MM/YYYY");
This is taking what ever was read in step 1 and trying to turn it into a string with the format of DD/MM/YYYY
CreateDate = moment(CreateDate)
Now you are parsing again without specifying the format so moment is doing it's best to guess
.format("YYYY-MM-DD");
Now you have told it to turn whatever it guessed the date to be into a string with the format YYYY-MM-DD
Do this instead:
var CreateDate = moment(jQuery('#ut-open-date').html(), 'DD/MM/YYYY').format('YYYY-MM-DD');
The moment(dateString) form is deprecated you should use the form moment(dateString, expectedFormat) instead.
See moment documentation here: http://momentjs.com/docs/#/parsing/string-format/
Thanks to athms for link
The problem is you need to tell moment.js what format for date string you want parse by specifying second parameter. See all the supported format. If your format is not listed (See the Open Issue for NON-ISO strings), you need to specify the date format parameter.
moment( jQuery('#ut-open-date').html(), "DD/MM/YYYY" )
DEMO
I solved it by using split as follows:
var CreateDate = jQuery('#ut-open-date').text();
var DateArray = CreateDate.split('/');
CreateDate = DateArray[2] + '-' + DateArray[1] + '-' + DateArray[0];
Hey, just wondering how to convert an HH:MM string into a javascript Date object. I have tried new Date(string); and myDate.setTime() but to no avail.
A side question could be: How to convert a string in HH:MM into milliseconds from Jan 1, 1970.
Thanks for your help in advance.
How about something like:
//using timestr '10:33:21', could also be '10-33-21'
var dat = new Date, time = timestr.split(/\:|\-/g);
dat.setHours(time[0]);
dat.setMinutes(time[1]);
in JavaScript, I'm using the datejs library. http://www.datejs.com/
If you include this library, you have a function called "parseExact" and you could use it like this:
var dateString = "10-12";
var date = new Date.parseExact(dateString, "hh-mm");
To get the miliseconds, you can download the file time.js from http://code.google.com/p/datejs/source/browse/trunk/#trunk/src. Then you have a function getTotalMilliseconds() you can use:
var mSeconds = date.getTotalMilliseconds();
I hope this will help a little bit.