Changing Date format in JavaScript [duplicate] - javascript

This question already has answers here:
Get String in YYYYMMDD format from JS date object?
(53 answers)
Closed 9 years ago.
var dt="29/05/2013"; //DD/MM/YYYY
I want to convert to yyyy/MM/dd format;
The approach I've used is:
var newdate=dt.getFullYear()+"/"+dt.getMonth()+"/"+dt.getDate();
Is there any simple approach to convert it with out using substring?
I don't want to use plugins by the way...

var dt = "29/05/2013";
var newdt = dt.split('/').reverse().join('/');
Fiddle:http://jsfiddle.net/4WGnC/

Consider using a library to help you: http://www.datejs.com/ and http://momentjs.com/ are good libraries.
Date.parse ("29/05/2013").toString("yyyy/MM/dd")

simply try like this
var a = dt.split("/");
var new_date = a[2]+'/'+a[1]+'/'+a[0];
alert(new_date);

If you are using jquery ui..
var newdate = $.datepicker.formatDate('yy/mm/dd', new Date());

Look this Example:
var d = new Date ( "January 6, 1972" );
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);
Hope it will help you!

Related

angularjs - calculate a date plus one day

I need to get the date one day after another date.
I do :
$scope.date2.setDate($scope.date1.getDate()+1);
if
$scope.date1 = 2015-11-27
then
$scope.date2 = 2015-11-28
It s ok,
but when
$scope.date1 = 2015-12-02
then
$scope.date2 = 2015-11-28 (ie tomorrow)
I don't understand why...
If anyone knows..
try this instead efficient simple pure JS
var todayDate = new Date();
console.log(new Date().setDate(todayDate.getDate()+1));
so you will have that same Date type object and hence you don't need to go with moment.js
Use moment.js for this momentjs
var startdate = "2015-12-02";
var new_date = moment(startdate, "YYYY-MM-DD").add('days', 1);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(new_date);
alert(day + '.' + month + '.' + year);

Get previous month date javascript [duplicate]

This question already has answers here:
How to get 30 days prior to current date?
(16 answers)
Closed 7 years ago.
How can I get previous month date in javascript. Suppose you have today's date like:
var abc = new date();
It will return today's date for example 03-11-2015. Now I want to get 03-10-2015. This is 30 days less than todays date. How can I do this?
var d = new Date();
d.setMonth(d.getMonth() - 1);
Check out momentjs, great little library for manipulating and formatting dates.
Complementing Robert Shenton's answer:
var d = new Date();
var newMonth = d.getMonth() - 1;
if(newMonth < 0){
newMonth += 12;
d.setYear(d.getFullYear() - 1); // use getFullYear instead of getYear !
}
d.setMonth(newMonth);

How to add months to a date of this format?

My date is in this format 2008-01-01(yyyy-mm-dd). I would like to add months to add format such that if I have 2008-01-01 + 13 would give me 2009-02-01. How can this be done in javascript?
var d = new Date('2008-01-01');
d.setMonth( d.getMonth() + 1 );
alert( d.getFullYear()+'-'+(d.getMonth() + 1 ) + '-'+ d.getDate());
You can add months to your date this way.
new Date(new Date(myDate).setMonth(myDate.getMonth()+13));
Simple as that:
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 13);
This is my own answer.
var moment = require('moment')
var myDate = new Date("2008-01-01");
myDate.setMonth(myDate.getMonth() + 13);
var answer = moment(myDate).format('YYYY-MM-DD');

javascript date is wrong format [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I have a function that gets a date from a jQuery calendar and then formats it in year-month-day.
The date I get for the calendar is 03/04/2013 for dateString and then I want it in the format of 2013-03-04. But the date I am getting for start_date is 2013-21-04. Strange because it had been ok, I think.
function makeUpDates() {
// concantenate values to date_start and date_end hidden inputs
var dateString = document.getElementById('date').value, date = new Date(dateString);
document.getElementById('date_start').value = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + ("0" + date.getDate()).slice(-2);
var numDays = document.getElementById('slider').value;
date.setDate(date.getDate() + parseInt(numDays));
var dateEnd = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + ("0" + date.getDate()).slice(-2);
document.getElementById('date_end').value = dateEnd;
}
You can convert the dates like this
//will take a date in the form MM/DD/YYYY and return YYYY-MM-DD
function convertDate(dateString){
var dateparts = dateString.split("/");
var newDate = dateparts[2]+"-"+dateparts[0]+"-"+dateparts[1]
return newDate;
}
Also for more general Date handling you can check out moment.js, mentioned in this answer to a more general question on this topic
start_date is 2013-21-04
Probably you forgot the parenthesis around date.getMonth()+1 in the string concatenation, yielding 21 instead of 3. It seems to be fixed in the snippet you posted though.

Javascript get and format current date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formatting a date in JavaScript
I need to show a current date in format like this (some examples):
sep 10, 2012
nov 5, 2012
and so on.
Using javascript I get a current date object
var date = new Date();
what I need to do next?
you can use this.
function dateTest(){
var d =new Date();
var month_name=new Array(12);
month_name[0]="Jan"
month_name[1]="Feb"
month_name[2]="Mar"
month_name[3]="Apr"
month_name[4]="May"
month_name[5]="Jun"
month_name[6]="Jul"
month_name[7]="Aug"
month_name[8]="Sep"
month_name[9]="Oct"
month_name[10]="Nov"
month_name[11]="Dec"
alert(month_name[d.getMonth()]+" "+d.getDate()+" , "+d.getFullYear());
}
You can use the getMonth (including some switch/case for the text), the getDate and the getFullYear methods to build your string.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/prototype#Methods
Use dateFormat lib available in following link
http://stevenlevithan.com/assets/misc/date.format.js
Refer this article for formatting js using above lib.
http://blog.stevenlevithan.com/archives/date-time-format
Edit:
If you cant use lib then
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var formattedDate = curr_date + " " + curr_month + ", " + curr_year;
Try this
function getCurrentDate(){
var now=new Date();
var date=now.getDate();
var year=now.getFullYear();
var months=new Array('jan', 'feb', 'mar' ... 'dec');
var month=months[now.getMonth()]
return month + ' ' + date + ', ' + year;
}
EDITED
You can use this function in your code :
function getFormattedDate(input){
var pattern=/(.*?)\/(.*?)\/(.*?)$/;
var result = input.replace(pattern,function(match,p1,p2,p3){
var months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Dec'];
return (months[(p1-1)]+" "+p2<10?"0"+p2:p2)+" "+p3;
});
alert(result);
}
And for ref. you can call this function directly like this:
getFormattedDate("11/18/2013");
OR you can use this code, too.
var date = new Date();
dateFormat(date,"mediumDate");
you can also find further different formats here.

Categories