[javascript] Using moment.js to convert date to string "MM/dd/yyyy"

I need to take the date value from jquery datepicker turn it into string format "MM/dd/yyyy" so it can do the right ajax post. When the page loads or upon changing the datepicker, a jquery ajax call is made.

I have this code:

var sTimestamp =
moment($("#start_ts").datepicker("getDate")).format("MM/dd/yyyy");

But it doesn't turn it into "MM/dd/yyyy". When I use fiddler to check what is sent down the wire, this is the body:

startTimestamp=03%2FTh%2Fyyyy&endTimestamp=03%2FTh%2Fyyyy&pageSize=50&pageNum=0

If I use the compose in fiddler and change the body to:

startTimestamp=03/13/2013&endTimestamp=03/14/2013&pageSize=50&pageNum=0

I get the right response. So, my question is, is there a way to take a date object and format it to a string "MM/dd/yyyy" using moment.js? Or is there something wrong with the way I get the date from datepicker?

Btw, I am assuming that datepicker.getDate returns a date object since that's what the jQuery docs tell me.

Thank you,

This question is related to javascript jquery momentjs

The answer is


Try this:

var momentObj = $("#start_ts").datepicker("getDate");

var yourDate = momentObj.format('L');

Use:

date.format("MM/DD/YYYY") or date.format("MM-DD-YYYY")}

Other Supported formats for reference:

Months:

M 1 2 ... 11 12

Mo 1st 2nd ... 11th 12th

MM 01 02 ... 11 12

MMM Jan Feb ... Nov Dec

MMMM January February ... November December

Day:

d 0 1 ... 5 6

do 0th 1st ... 5th 6th

dd Su Mo ... Fr Sa

ddd Sun Mon ... Fri Sat

dddd Sunday Monday ... Friday Saturday

Year:

YY 70 71 ... 29 30

YYYY 1970 1971 ... 2029 2030

Y 1970 1971 ... 9999 +10000 +10001


.format('MM/DD/YYYY HH:mm:ss')

this also might be relevant to anyone using React -

install react-moment (npm i react-moment)

import Moment from 'react-moment'


<Moment format="MM/DD/YYYY">{yourTimeStamp}</Moment>

(or any other format you'd like)


StartDate = moment(StartDate).format('MM-YYYY');

...and MySQL date format:

StartDate = moment(StartDate).format('YYYY-MM-DD');

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to momentjs

How to get am pm from the date time string using moment js Vuejs and Vue.set(), update array How to subtract one month using moment.js? Get timezone from users browser using moment(timezone).js Check if date is a valid one moment.js get current time in milliseconds? Deprecation warning in Moment.js - Not in a recognized ISO format Moment js get first and last day of current month Moment get current date Moment.js - How to convert date string into date?