Insert date into array - javascript

I have inserted random date into a textbox let it be 12/2/2009 now I want only 2 in this how to do it with javascript how do I need array or any other method.
Please give me an appropriate example

Use :
var mydate = $("textboxId").val();
var dateArr = mydate.split("/");
var date = dateArr[0];
var month = dateArr[1];
var year = dateArr[2];

You'll get month from 'dd/m/yyyy' formatted date by reversing the date
new Date("12/2/2009".split("/").reverse().join("/")).getMonth()
Check here

Related

How do I get the time in milliseconds from 2 different string?

I have the following code which I get from parameters in the URL.
This is what I have in the URL
&dateStart=15.01.2015&timeStart=08%3A00&
After getting the parameters I have the following: 15.01.2015:08:00
Using Javascript how can I parse this string to get the date in milliseconds?
Date.parse(15.01.2015:08:00)
But obviously this doesn't work.
Date.parse(15-01-2015)
This works and I can change this but then how do I add or get the milliseconds from the time??
This is quite possibly the ugliest JavaScript function I've written in my life but it should work for you.
function millisecondsFromMyDateTime(dateTime) {
var dayMonth = dateTime.split('.');
var yearHourMinute = dayMonth[2].split(':');
var year = yearHourMinute[0];
var month = parseInt(dayMonth[1]) - 1;
var day = dayMonth[0];
var hour = yearHourMinute[1];
var minute = yearHourMinute[2];
var dateTimeObj = new Date(year, month, day, hour, minute, 0, 0);
return dateTimeObj.getTime();
}
It will work with the format that your DateTime is in aka day.month.year:hours:minutes.
You can achieve it using Javascript Date Object and JavaScript getTime() Method:
var dateString="01.15.2015 08:00";
var d = new Date(dateString);
console.log(d);
var ms=d.getTime();
console.log(ms);
ms+=10000;
console.log(new Date(ms));
Here is a DEMO Fiddle.
Note: Change your date string from 15.01.2015:08:00 to "01.15.2015 08:00" because it's not a valid Date format.
Check for format
Date() in javascript :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Format allowed :
https://www.rfc-editor.org/rfc/rfc2822#page-14
You can try to use moment.js library like this:
moment('15.01.2015 08:00', 'DD.MM.YYYY HH:mm').milliseconds()
Just for the sake of completion, you can always extract the information and create a Date object from the extracted data.
var dateStart = '15.01.2015'
var timeStart = '08:00';
var year = dateStart.substring(6,10);
var month = dateStart.substring(3,5);
var day = dateStart.substring(0,2);
var hour = timeStart.substring(0,2);
var mins = timeStart.substring(3,5);
var fulldate = new Date(year, month-1, day, hour, mins);
console.log(fulldate.getTime());

How to change the date format in jquery?

I have a variable which stores the date as "18/07/2013". I need to parse this to "07/18/2013". How this is possible in jquery?
var date = $("#dte").val();
var date = $("#dte").val();
d=date.split('/');
newdate=d[2]+"/"+d[1]+"/"+d[0];
return newdate;
You could use
var date = $("#dte").val().replace(/^(\d\d)\/(\d\d)/, "$2/$1");
to swap month and date part.
A simple replace would do the trick:
date = date.replace(/^(\d\d)\/(\d\d)\//, "$2/$1/");
No need of jQuery for this.
Try this:
var date = $("#dte").val().split('/');
var newDate = date[1]+'/'+date[0]+'/'+date[2];
alert(newDate); // or $("#dte").val(newDate); if you want to update the input
Try this out.
It helped me to format date in jQuery
$.datepicker.formatDate('dd/mm/yy', new Date(yourDateString))
There is jQuery dateFormat jquery plugin..you can use that for show date format.
var date = date.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "$2/$1/$3");
JSFIDDLE
javascript's Date accepts a string in the format year / month / day, so just reverse the order and create a date object :
var date = '18/07/2013';
var dObj = new Date(date.split('/').reverse().join('/'));
var newD = dObj.getDate() + '/' + (dObj.getMonth()+1) + '/' + dObj.getFullYear();
FIDDLE

Parsing a date in JavaScript?

Is there a way I could get the year, month (0 based) and day from '03/05/2013'
If so, how?
Thanks
Is there a safe way to do it that can check if it is in the correct format?
You have the Date.parse method which parses a Date string and returns its timestamp, so you can call new Date().
Something like this:
new Date(Date.parse('03/06/2013'))
Most easy is using the split() function, i think:
var date = "03/05/2013";
var dateParts = date.split("/");
var day = dateParts[0];
var month = dateParts[1];
var year = dateParts[2];
http://jsfiddle.net/s7ma2/1/

How to Calculate Number of days between two calender dates which are in text format in Javascript?

I'm having a form which is having two input fields with date picker(with the help of jquery ui).One asking a check in date & the other asking check out date.I want to calculate the Number of Days Between those two dates.I am a beginner to javascript.So can any one help me?
Use unix timestamp. It's universal tool when you working with time:
var a = new Date(2012,11,10);
var b = new Date(2012,11,12);
var dt = (b.getTime() - a.getTime())/(24*3600*1000) //2
You must first get the value of such input fields, eg:
var v1 = document.getElementById('input1Id').value;
var v2 = document.getElementById('input2Id').value;
then you have to instantiate two new Javascript Date objects:
var parts1 = v1.split('/');
var parts2 = v2.split('/');
// this will split your string into date parts, eg. 11/30/2012 would result as an array ['11','30','2012'];
var date1 = new Date(parts1[2],parts1[0],parts1[1]);
var date2 = new Date(parts2[2],parts2[0],parts2[1]);
// remember to check the index of array items considering your date format
finally, you just have to subtract one date from the other to get the difference in days:
var difference = Math.Abs(date1 - date2);
difference = parseInt(difference / 86400000);
//86400000 are the milliseconds in one day. Parsing to int will round the day - eg.5,4 days results as 5 day
Edit: Sure, you can reference your html element by his id attribute
<input type="text" id="myTextbox">
---
var txb = document.getElementById('myTextbox');
// do anything else to your txb here, if u like
txb.value = difference;
that's it!

JS - Compare which date is older

I want to compare two dates as which is bigger in those dates.
var date1 = 2011-9-2;
var date1 = 2011-17-06;
Can anybody say how can I do this?
You'll need to convert both strings to date objects first.
var date1 = new Date('2011-09-02');//yyyy-mm-dd format
var date2 = new Date('2011-06-17');
if(date1 > date2){
alert('date1 is bigger than date2');
}
Once you have the 2 variables as date objects you can compare them against each other (without needing to convert to milliseconds/minutes/?)
Check this link
And then do something like this:
var days = 0;
var difference = 0;
Christmas = new Date("December 25, 2005");
today = new Date();
difference = Christmas - today;
days = Math.round(difference/(1000*60*60*24));
Code source
Create Date objects from your two values (check this link) and use that to do the comparison.

Categories