How to split the date , time using javascript? - javascript

How to split the date,time using java script ? I am getting date,time 2010-05-04 20:13:12.0.But i need only date.
date format mm/dd/yyyy
var cdate = gridline[i=1]["CDATE"];
Now i need cdate as mm/dd/yyyy only.
Please help me.

function getDateFromString(datestring) {
var d = new Date(datestring);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
return curr_date + "-" + curr_month + "-" + curr_year;
}
Source: webdevelopersnotes.com

Related

Converting string m/dd/yyyy HH:MM:SS to date dd-mm-yyyy in Javascript

I have a string that looks like '1/11/2018 12:00:00 AM' and I want to reformat it to dd-mm-yyyy.
Keep in mind that the month can be double digit sometimes.
You can use libraries like moment.js. Assuming either you do not want to use any external library or can not use it, then you can use following custom method:
function formatDate(dateStr) {
let date = new Date(dateStr);
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return day + '-' + month + '-' + year;
}
console.log(formatDate('1/11/2018 12:00:00 AM'));
You can do somethink like this :
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
console.log(curr_date + "-" + curr_month + "-" + curr_year);
However best way is with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.
example:
var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');
function convertDate(oldDate) {
var myDate = new Date(Date.parse(oldDate)); //String -> Timestamp -> Date object
var day = myDate.getDate(); //get day
var month = myDate.getMonth() + 1; //get month
var year = myDate.getFullYear(); //get Year (4 digits)
return pad(day,2) + "-" + pad(month, 2) + "-" + year; //pad is a function for adding leading zeros
}
function pad(num, size) { //function for adding leading zeros
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
convertDate("1/11/2018 12:00:00 AM"); //11-01-2018
Demo here

JavaScript date and time formatting,

I have read a lot of thread about the subject, but i'm not much into programming... i've made some tests but never acheive what i wanted.
Here we have a html template we have to modify to suit our needs.
here the code i have to edit:
<p id="DATE"></p>
<script>
var d = new Date();
document.getElementById("DATE").innerHTML = d.toString();
</script>
Its giving me the date OK...but not i the format i want...
the format i want is... DD-MM-YYYY HH:MM
Simple has that :)
Its been a couple of hours and i can't do it... Can anyone help with a code i just can copy/paste in my html file.
As opposed to using an external JavaScript library for a simple task. You should be able to achieve the same with Vanilla js.
Refer to this:
Where can I find documentation on formatting a date in JavaScript?
In short:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
console.log(curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min);
The above snippet should have all the code you need.
For your specific implementation, you will need to do the below:
<script>
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var formattedDate = curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min;
document.getElementById("DATE").innerHTML = formattedDate;
</script>
A function for the format you require is fairly simple. The following will return a string for a provided Date, or the current date if none passed.
function formatDate(date) {
var d = date || new Date();
function z(n){return (n<10?'0':'')+n}
return z(d.getDate()) + '-' +
z(d.getMonth()+1) + '-' +
d.getFullYear() + ' ' +
z(d.getHours()) + ':' +
z(d.getMinutes());
}
document.write(formatDate());
BTW, you can't use the same token for two separate parts. While there is no standard or even consistency for formatting tokens, your format would be more commonly expressed as:
DD-MM-YYYY HH:mm
Which is compatible with moment.js and a few others, though there are many other token sets to represent the same format.
To achieve your goal simply try following codes:
First of all, inside the head tag add the below line of code:
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
Finally, change your existing line of code:
document.getElementById("DATE").innerHTML = moment(d).format('DD-MM-YYYY HH:MM');
Happy coding :)

loop through date and print formated date

for(i=0;i<30;i++){
}
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
I'm stuck, how can I print out the next 30 days?
like 3/3/2015, 4/3/2015, 5/3/2015 and so on..
var d = new Date();
var curr_month = d.getMonth();
curr_month = curr_month + 1;
var curr_date = d.getDate();
for( i = curr_date; i <= 31; i++ ){
var curr_year = d.getFullYear();
var date = curr_date + '/' + curr_month + '/' + curr_year;
curr_date++;
console.log(date);
}
Try this.It worked for me.
demo link
Try this:
var d = new Date();
var curr_day = d.getDate();
for(i=0;i<30;i++){
curr_day = curr_day+1;
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var newDate = new Date(curr_year,curr_month,curr_day)
var formatedDate = newDate.getDate() + '/' + newDate.getMonth() + '/' + newDate.getFullYear();
formatedDate;
}
You need to increment the day variable
You could put all that code below the loop inside the loop, then for each iteration add one day to the Date object. (add 24 * 60 * 60 * 1000)
var ONE_DAY = 24 * 60 * 60 * 1000;
var d = new Date();
for(i=0;i<30;i++) {
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
d = new Date(d.getMilliseconds() + ONE_DAY);
}
I would probably make slightly different style choices, but this is one way of doing it that pertains to the code you wrote.
Also, I don't have enough reputation to comment, but the other solution by pedrumj is very similar to mine, but keep in mind that it actually skips the first day (the new date should be assigned at the end of the loop)
Demo here
Use this
var d = new Date();
var curr_month = d.getMonth();
for(i=0;i<30;i++){
var curr_date = d.getDate();
var curr_month = curr_month + 1;
var curr_year = d.getFullYear();
var Printdate = curr_date + '/' + curr_month + '/' + curr_year;
console.log(Printdate);
}
You can reduce the date field by 1 day on each loop as follows:
var d = new Date(1425356823380);
for(i = 0; i < 30; i++){
var date = d.toLocaleDateString();
// code
d.setDate(d.getDate()+1);
}
Here is a fiddle
Your for loop doesn't loop through anything. You should set your fixed variables first, then loop through your variable you wish to change.
for(i=0;i<30;i++){
}
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var formatedDate = curr_date + '/' + curr_month + '/' + curr_year;
Like this:
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var curr_day = d.getDay();
var formatedDate = curr_month + '/' + curr_day + '/' + curr_year;
for(i=0;i<30;i++){
curr_day++;
formattedDate = curr_month + '/' + curr_day + '/' + curr_year;
console.log(formattedDate);
}
Mind, February only has 29 days at most.
See the Docs for more information. You may just want to use a library that can handle all the strange edge cases that happen with dates, such as MomentJS
for(var i=0;i<30;i++){
var start = new Date();
var next = new Date();
next.setDate(start.getDate() + i);
var date_str=next.getDay()+"/"+next.getMonth()+"/"+next.getFullYear()
console.log(next);
}
You can format start, next date as per your need.

Converting date format from mm/dd/yyyy to yyyy-mm-dd format after entered

In my datepicker the date will be inserted in mm/dd/yyyy format. But after I inserted I want it to be sent in yyyy-mm-dd format. I am using JavaScript to do this. But I wasn't able to do that. So what should I do?
Thanks & regards,
Chiranthaka
you could also use regular expressions:
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
The expression also works for single digit months and days.
I did the opposite for my website, but it might help you. I let you modify it in order to fit your requierements. Have fun !
getDate
getMonth
getFullYear
Have fun on W3Schools
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
if(curr_month < 10)
curr_month = "0"+curr_month;
if(curr_date < 10)
curr_date = "0"+curr_date;
var curr_date_format = curr_date+"/"+curr_month+"/"+curr_year;
Adding more to Christof R's solution (thanks! used it!) to allow for MM-DD-YYYY (- in addition to /) and even MM DD YYYY. Slight change in the regex.
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})[\/ -](\d{1,2})[\/ -](\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
As Christof R says: This also works for single digit day and month as well.
// format from M/D/YYYY to YYYYMMDD
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
return "".concat(yyyy).concat(mm).concat(dd);
};
var siku = new Date();
document.getElementById("day").innerHTML = siku.yyyymmdd();

Date Conversion in Javascript [duplicate]

This question already has answers here:
convert date from dd/mm/yyyy to yyyy-mm-dd in javascript
(4 answers)
Closed 9 years ago.
How to convert date format in Javascript like
(MM-dd-YYYY) to (YYYY-MM-DD)
please suggest some standard format for conversion
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var newDate = curr_year + "-" + curr_month + "-" + curr_date ;
This should work fine
var str = "01-17-2021";
var arr = str.split("-");
var dateFormatted= arr[2]+"-"+arr[0]+"-"+arr[1];
Look at this link.
http://jsfiddle.net/9C2p8/
var start = new Date('10-10-2013');
var startDate = $.datepicker.formatDate('yy-mm-dd', new Date(start));
alert(startDate);

Categories

Resources