How to change the date format in JavaScript? - javascript

I am using one jquery date picker,
with using picker i am getting date in like this format
Friday, May 21, 2010
Now i want to add one day in this date so i think, i can only do if i change the date in exact format like
21/5/2010
I want to only convert that bcz i want to add one day to the particular date.
So what do u suggest me? How can I do that?
Can i do without converting it ?
thanks in advance....

Take a look at http://docs.jquery.com/UI/Datepicker#option-dateFormat

datejs may be useful to you.

In addition to the formatting options given by others, you should add using date objects rather than to the string representation of the date object.
I.E.
// add 5 days to today
var myDate=new Date();
myDate.setDate(myDate.getDate()+5);

I think you need to detail what jQuery plugin do you use.
Is it this one? http://jqueryui.com/demos/datepicker/
If so, then when you cann getDate method, you'll get Date object representing a date. You can easily manipulate it.

The date format has nothing to do with how dates are stored; it only affects the way dates are displayed. JavaScript has a native Date object and jQuery UI's Datepicker allows to access such object:
http://jqueryui.com/demos/datepicker/#method-getDate
Once you have a Date object, you can alter it to suit your needs:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
Finally, you can feed it back into Datepicker:
http://jqueryui.com/demos/datepicker/#method-setDate

Related

Kind of time (yyyymmddThhmmZ)

I'm trying to generate links to Google calendar and see this tool:
https://decomaan.github.io/google-calendar-link-generator/
And the links are generated as:
https://www.google.com/calendar/render?action=TEMPLATE&text=Appointment+with+VULKOVICH%2C+BILL&details=a+Description&location=a+Location&dates=20210105T103300Z%2F20210114T103300Z
and as you can see the dates are like:
20210105T103300Z
and I am trying to convert this to my own dates but I don't know which type is this and how to format. I have the dates both, in moment or in date, but don't know how to convert.
That's ISO-8601
The first part is the date in year-month-date order, the second part is the time and the final letter indicates the timezone (here Z for 'Zulu')
Since you're using moment.js: moment().utc().format('YYYYMMDDTHHmmss[Z]'), or without a library new Date().toISOString().replace(/\W/g,'').replace(/\d{3}Z/,'Z'). This is really a duplicate of How to format a JavaScript date.
Source: comment by RobG Jan 11 at 12:58

Bootstrap datepicker setStartDate not working properly?

I am using Bootstrap datepicker and on selecting different values from a list its startdate is changing. It is working fine if I set the startdate 2013 from 2008 but it doesn't work if a select start date 2008 and currently its 2013.
What could be the reason here?
$('#datepicker').datepicker('setStartDate', updatedDate);
This line I am executing whenever I select different startDate.
Really need to know what updatedDate value is.
However, if you read the docs for the dtepicker the value passed in must be a string that is understandable by format
https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#setstartdate
https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#startdate
Date or String. Default: Beginning of time
The earliest date that may be selected; all earlier dates will be
disabled.
Date should be in local timezone. String must be parsable with format.
So what you pass in as the format option must match the format of your start date. If you do not set the format optin, the default is "mm/dd/yyyy"
Without seeing code, I can only hypothesize; try calling [...].datepicker('update', 'date_string'); on the object to force an update on the control.

How can I change formatting of date in javascript?

How can I change a date from the form 01/01/02 to January 1st, 2002?
Can I do this in javascript? If not, jQuery?
While you can certainly write your own date formatting routine, your best choice is using a library like moment.js. That is especially true if you'd be doing some manipulations to those dates besides just displaying them. Dates are hard to do and there are many things to consider (i.e. timezones)...
Using momentjs:
var x = moment('01/01/02', 'mm/dd/YYYY').format('MMM Do, YYYY');
console.log(x);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
dateObj.toLocaleDateString() in javascript might be helpful

Simplest way to Convert to invalid date to a moment object

I have store hour information that is given like this
Sunday 8:00
Sunday 22:00
Since it is store hour information the year, month, and date do not matter but unfortunately I still need to this information to be a moment object for formatting/ Organisation reasons.
What is the simplest way to convert this invalid date format into a moment object.
I've heard of sugar and it seems perfect but my team will not want to have to install it for just one simple thing, so no libraries unless it is something included in angularjs.
Specify the input format:
moment("Sunday 8:00", "dddd hh:mm");
Reference
If I understand you properly you already have the Date javascript Object. Then you can just set it using:
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day); //or your JS Date object.
You can read it at Moment.js doc: http://momentjs.com/docs/#/parsing/date/

javascript date manipulation

I have a string
2010-08-02 12:13:06.0
and need to get something like
Fri Aug 6 2010
out of it (the input does not map to the output for the values I gave, just examples)
I fear Im going to have to do some string manipulation to get what I want; the js Date object does not seem to have methods capable of parsing the input string.
Is this correct?
We are using jquery, but cant find anything in that library that would help...
Everything has been invented before us:
http://www.mattkruse.com/javascript/date/
You can use the date object for this. Just parse the first part of the date string to get the individual numbers and use setFullYear(), setMonth(), setDate(). You will have to subtract 1 from the month, but then use the toDateString() and it outputs it like your example. http://www.w3schools.com/jsref/jsref_obj_date.asp

Categories

Resources