Get todays date in Javascript - javascript

How can I get the current date in Javascript in this format?
"M/D/YYYY"?
Thanks.
If this would be today it would be
"2/17/2011", if it was the 3rd it would be "2/3/2011".
Thanks

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
I assigned each part into its own variable for this example so that it's more clear as to what it returns.

Use the javascript Date object:
var d = new Date();
alert((d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear());

Steven Levithan's (stevenlevithan.com) dateFormat function looks really versatile to me.
See:
http://blog.stevenlevithan.com/archives/date-time-format
In his code, he adds dateFormat in as a prototype method for Date.
// For convenience...
Date.prototype.format = function (mask, utc) {
return dateFormat(this, mask, utc);
};
So you can use it as a method on a Date object.
var now = new Date();
var variable=now.format("m/dd/yy");
document.write(variable);
HTH
Rich

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);

How do I convert timezone date to correct local date?

I have this date:
2015-05-28T23:00:00.000Z
I need to convert it to the local date which would be (in this format):
29/05/2015
I would expect the above formatted date to be correct based on the date string above.
How would I do this?
Thank you
convert it to Date object:
var dateString = '2015-05-28T23:00:00.000Z';
var date = new Date(dateString)
then you cant format it:
var formatedDate = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
But you can also use moment.js
moment(dateString).format('DD/MM/YYYY');
It's been well covered elsewhere that using the Date constructor to parse strings isn't a good idea. The format in the OP is consistent with ES5 and will be parsed correctly by modern browsers, but not IE 8 which still has a significant user share.
Parsing the string manually isn't difficult:
function isoStringToDate(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5], b[6]));
}
Then to format it:
function dateToDMY(d) {
function z(n){return (n<10?'0':'') + n}
return z(d.getDate()) + '/' + z(d.getMonth()+1) + '/' + d.getFullYear();
}
console.log(dateToDMY(isoStringToDate('2015-05-28T23:00:00.000Z'))); // 29/05/2015
To be consistent with ES5, the parse function should check values aren't out of range but if you're confident of the correctness of the string that shouldn't be necessary.
Thank you for your replies.
I've got it formatted by doing:
var d = new Date('2015-05-28T23:00:00.000Z');
var n = d.getDate() + '/' + (d.getMonth() +1 ) + '/' + d.getFullYear();
document.getElementById("demo").innerHTML = n;
Please add the following code in script
<script>
var d = new Date("2015-05-28T23:00:00.000Z");
var str=d.toString();
var date = new Date(str),
mnth = ("0" + (date.getMonth()+1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
var local_date=[ date.getFullYear(), mnth, day ].join("/"); //yyyy/mm/dd
</script>
Hope it works.Thank you

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.

How can I format a json date in dd/mm/yy format in javascript?

I have a json date like \/Date(1334514600000)\/ in my response and when I convert it in javascript then I got this date Tue Apr 17 2012 11:37:10 GMT+0530 (India Standard Time),
but I need the date format like 17/04/2012 and I fail every time. Can anyone tell me how can I resolve it?
I don't think that the other posted answers are quite right, you have already accepted one as working for you so I won't edit it.
Here is an updated version of your accepted answer.
var dateString = "\/Date(1334514600000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);
It uses a technique from this answer to extract the epoch from the JSON date.
I found very helpful the row1 answer, however i got stuck on the format for input type="date" as only returns one string for decimals under 10, I was able to modify to work on input type="date", I basically adapted the code from row1 to the code from the link http://venkatbaggu.com/convert-json-date-to-date-format-in-jquery/
I was able through jquery .val add the date to the input
var dateString = "\/Date(1334514600000)\/".substr(6);
var currentTime = new Date(parseInt(dateString));
var month = ("0" + (currentTime.getMonth() + 1)).slice(-2);
var day = ("0" + currentTime.getDate()).slice(-2);
var year = currentTime.getFullYear();
var date = year + '-' + month + '-' + day;
alert(date);
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = day + "/" + month + "/" + year
alert(date);
It's answer to your question...
Build the date object with your timestamp
var currentTime = new Date(1334514600000)
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = day + "/" + month + "/" + year
alert(date);​
it works
http://jsfiddle.net/ChgUa/
//parse JSON formatted date to javascript date object
var bdate = new Date(parseInt(emp.Birthdate.substr(6)));
//format display date (e.g. 04/10/2012)
var displayDate = $.datepicker.formatDate("mm/dd/yy", bdate);
Easiest way of formatting date is by using pipes if you are using Angular.
Click here
//in .ts file
ngOnInit() {
this.currentDate = new Date()
}
//in html file
<p>Current date is:</p>{{currentDate | date: 'dd/MM/yyyy'}}
//Output: 22/04/2020
Here is an updated version of your accepted answer. DD/MM/YYYY Format Get Try This..
var dateString = "/Date(1623781800000+0530)/"+.substr(6);
var currentTime = new Date(parseInt(dateString));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
if (month.toString().length == 1)
month = "0" + month.toString();
if (day.toString().length == 1){
day = "0" + currentTime.getDate();}
var datenew = day + "/" + month + "/" + year;
var Date = new Date(Tue Jun 15 2021 23:52:47 GMT+0800 (Malaysia Time)).toDateString(); console.log(Date);
Result == Tue Jun 15 2021

How to convert dateTime format in javascript

How i could convert datetime 5/8/2011 12:00:00 AM (m/d/yyyy) to dd-MMM-yyyy like 08-May-2011 in javascript.
This link is a good resource you can use for.
http://blog.stevenlevithan.com/archives/date-time-format
Alternatively, you need to get the individual part and concatenate them as needed like below.
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var monthnumber = now.getMonth();
var monthday = now.getDate();
var year = now.getYear();
String myOutput = monthday + "-" + monthnumer + "-" + year;
To get the month name instead of month number, you need to define an array like below
var arrMonths = new Array ("Jan","Feb"....};
String myOutput = monthday + "-" + arrMonths[monthnumer-1] + "-" + year;
check below link hope you got some idea
http://bytes.com/topic/javascript/answers/519332-how-convert-datetime-format-using-javascript
http://blog.stevenlevithan.com/archives/date-time-format
similar question solution

Categories