Converting date format from M/D/YYYY to Month D, YYYY - javascript

A page on my SharePoint site has a Date field with a datepicker. The selected date is inputted in M/D/YYY format (eg, 5/7/2013).
But I need the date to display like this once the page is published: MAY 7, 2013.
Is there a way to accomplish this with jQuery?

You can use the datepicker only for this:
var date = $.datepicker.formatDate('MM d, yy', new Date());
alert(date);
DEMO
UPDATE
var myDate = $("#datepickerID").datepicker('getDate');
var date = $.datepicker.formatDate('MM d, yy', new Date(myDate));
alert(date);
UPDATE 2
function GetNewDate(myDate) {
var date = $.datepicker.formatDate('MM d, yy', new Date(myDate));
return date;
}
Also, set the p like this:
$('p').text(GetNewDate(date));

Related

How can I correctly set the default value for bootstrap 3 datetimepicker?

I am trying to set the default date/time for a bootstrap 3 datetimepicker
to today's date 08:00 AM.
The following code is what I have done. However, that does not seems to be setting the datetime for the field.
$(function(){
$('#started-at').datetimepicker({
format: 'MM/DD/YYYY LT',
useCurrent: false,
defaultDate: getDefaultStartAt()
});
});
function getDefaultStartAt()
{
var d = new Date();
var month = d.getMonth();
var day = d.getDate();
var year = d.getFullYear();
return new Date(year, month, day, 8, 0);
}
How can I correctly set the default value for bootstrap-datetimepicker?
Your js code is fine.
You probably didn't include (or in wrong order) something from minimal requirements. moment.js maybe?
Or your html could be wrong.
Here you can see working example with your code: https://jsfiddle.net/5vv9chgx/

Inbuild Functionality to Format Dates using Jquery

I have a requirement to show a date in the format as below in the label control.
Wednesday 20 Jul 2016 11:31 AM
Is there any in build functionality available with Jquery without righting my on function to build the string in the specific way as above?
You can use date.format library:
var dateFormat = require('dateformat');
var now = new Date();
dateFormat(now, "dddd dS mmmm yyyy h:MM TT");
It will return
Wednesday 20 Jul 2016 11:31 AM
Use the dateFormat library by Steven Levithan. It's just 1.2kb when minified and gzipped.
Here's how to use it:
var _date = dateFormat(new Date(), "dddd dd mmm yyyy hh:MM TT"); //use _date variable to display
You can also use this JQuery library : Date Format
var now = new Date();
document.write($.format.date(now, "ddd dd MMM yyyy hh:mm a"));

Convert date so that jquery calender value can be set as the date

I have a date in the format: 2014-11-21T00:00:00.000Z that I need to place into a jquery calender/datepicker.
I have been trying to set it using:
document.getElementById("startdate").valueAsDate(2014-11-25T00:00:00.000Z)
AND
$('#startdate').val("2014-11-25T00:00:00.000Z")
But to no avail. How can I populate the date picker with the date?
Use the setDate method:
$('#startdate').datepicker();
var dateText = "2014-11-25T00:00:00.000Z";
var actualDate = new Date(dateText);
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() + 1);
$('#startdate').datepicker('setDate', newDate);
FIDDLE
Also note, javascript day numbers start at 0 which is why we +1 on the day.

How to format datestring to d, MMM yyyy in javascript

I got this:
function parseDate(s)
{
var d = s.split(/\D/);
return new Date(d[2], --d[1], d[0]);
with the calendar tag like this:
<p:calendar
id="testDate"
styleClass="calendar"
pattern="d MMM, yyyy"
maxlength="10"
onchange="$(this).val(parseDate($(this).val()))"
onfocus="$(this).mask('99/99/9999');"
>
<p:watermark for="testDate" value="mm/dd/yyyy" />
</p:calendar>
I need to manually parse a date from 'dd/mm/yyyy' to 'd, MMM yyyy' but with the function above the result is e.g. "Wed, Aug 09 1995 00:00:00" hence could someone help me and tell how how i could change the format so that the string produced would be d, MMM yyyy?
I know this should be a very basic task however i am still learning how to code better, therefore all your help and explanations are greatly appreciated!
Take a look at momentjs you can use it to parse your date and format it into whatever format you'd like.
Lazy method: use the .toString()
function parseMyDate(s){
var dateParts = s.split("/");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
var dateStrParts = date.toString().split(" ");
return (date.getDate() + ", " + dateStrParts[1] + " " + dateStrParts[3]);
}
console.log(parseMyDate("09/06/2014"));
JSFiddle Demo

Date is interpreted as being in a different format

My code :
$('#datepicker').datepicker({
inline: true,
onSelect: function(dateText, inst) {
var d = new Date(dateText);
alert("DateText=>"+ dateText + "," + "DATE =>" + d);
$('#calendar').fullCalendar('gotoDate', d);
}
});
})
My error :
My problem is that the function is being passed dateText from fullCalendar in the form dd/mm/yyyy...it is then creating a new date using this and it is messing it up, thinking that the date should be in the form mm/dd/yy. Can anyone help me fix this please?
You are alerting the dateText but passing a date object to calendar. Look at FullCalendar API for format of 'gotDate' argument. You can use
d.getFullYear(), d.getMonth() and d.getDate() to convert
Try this,may be it will little helpfull for you
var date = $('#datepicker').datepicker({ dateFormat: 'mm-dd-yy' });
If you're looking to format the date. Try date format
edit:
maybe your date variable is wrong. Try
<div id="datepicker"></div>
var date = $('#datepicker').datepicker('getDate');
$.datepicker.formatDate('mm-dd-yy', date);

Categories

Resources