$scope.notAvailableDayClick=function(val1,date){
console.log("day clcik")
var startDate=$filter('date')(date,'yyyy-MM-dd')
var endDate=new Date(startDate)
endDate.setMinutes(59)
endDate.setHours(23)
}
date is 2015-01-16
if I do this
new Date(date)
Thu Jan 15 2015 16:00:00 GMT-0800 (Pacific Standard Time)
So I have to go with AngularJS
var startDate=$filter('date')(date,'yyyy-MM-dd')
but now I need startDate.getTime(), error occur I think it takes it as a String
As per angular docs the filter returns a String in requested format. Date constructor accepts ISO8601 formats usually although some browsers support many formats as I remember. Probably your format yy-MM-dd is not supported.
I hope the variable date is a valid Date object, in that case why don't you use it instead of the formatted string you made with angular filter?
var endDate = new Date(date);
endDate.setMinutes(59);
endDate.setHours(23);
Also you have a Date constructor that accepts the format
new Date(year, month[, date[, hour[, minutes[, seconds[, milliseconds]]]]]);
So if what you have in hand is 2015-01-16 you can get midnight of that day with:
var startDate = "2015-01-16";
var year = parseInt(startDate.split('-')[0], 10);
var month = parseInt(startDate.split('-')[1], 10) - 1;
var year = parseInt(startDate.split('-')[2], 10);
var endDate = new Date(year, month, date, 23, 59);
Just use the original date to create endDate not the angular filtered version
var endDate=new Date(date);
endDate.setMinutes(59);
endDate.setHours(23);
Best option is to use ISO-String, because Google Chrome supports this format: MM-dd-yyyy. In Mozilla this format gives Invalid Date.
new Date('MM-dd-yyyy')
So using Iso-String in Angular, it can done as follows:
new Date($filter('date')(yourdDate,'yyyy-MM-ddTHH:mm:ss.sssZ'))
Related
Firstly, I convert a datetime to date and time respectively.
var d = new Date();
var localeDate = d.toLocaleDateString();
var localeTime = d.toLocaleTimeString();
However, when i try to convert it back to date object
var convertLocaleDate = new Date(localeDate); // success
var convertLocaleTime = new Date(localeTime); // not a valid date format
How can i convert localeTime back to valid date?
look carefully. You are not converting localeDate back to the original date. The time portion is set to 00:00:00
You cannot achieve the result you are trying for.
new Date() will work for the arguments mentioned below
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
more info here :
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
giving localeTime as an argument doesn't work because the Date constructor will not know what date/month/year that time is for.
On the other hand localeDate worked because the date/month/year were known and time was set as default to 12am as default
This works:
var myDateString = '19th sep 2015';
myDateString = myDateString.replace('st','');
myDateString = myDateString.replace('th','');
myDateString = myDateString.replace('nd','');
myDateString = myDateString.replace('rd','');
var date = new Date(myDateString);
But is there a cleaner way? Can I pass the date format (including the ordinal part) to the Date constuctor?
I wouldn't rely on it parsing correctly for all people - for instance, people in France might have their locale set to French (surprise) and that would fail to parse apr for instance, because for them it's avr.
Instead, parse it yourself:
var myDateString = '19th sep 2015';
var parts = myDateString.split(" ");
var date = parts[0].replace(/\D/g,''); // keep only numbers
var month = "janfebmaraprmayjunjulaugsepoctnovdec".indexOf(parts[1].toLowerCase())/3;
var year = parts[2];
var date = new Date(year, month, date);
// note because of how we got the month, it's already conveniently zero-based
Use Moment.js library and use your date string like this:
moment('19th sep 2015', 'Do MMMM YYYY').format("D MMMM YYYY")
Output:
"19 September 2015"
The easiest way is to use a library like moment.js or the date part of sugar.js.
To do this properly by hand is not fun.
Update
If you're in full control of the dates, just use ISO 8601 date format, which the constructor of Date understands in any browser.
I am trying to compare two dates using javascript,The datetime format is given below.Here i want to check date1 > date2.How can i achive this in javascript.
var date1='2014-03-25 07:30 AM';
var date2='2014-03-25 04:30 PM';
Use getTime:
if ((new Date(date1).getTime()) > ( new Date(date2).getTime())){
}
Like this -
if((new Date(date1).getTime()) > (new Date(date2).getTime())){
// do something
}
Please try this.
if (new Date(date1) > new Date(date2))
This should work:
var date1 = '2014-03-25 07:30 AM';
var date2 = '2014-03-25 04:30 PM';
console.log((new Date(date1)) >= (new Date(date2)));
EDIT:
Op correctly pointed out that the code above does not work (in FireFox, for example, but it works in Chrome). I have read that
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
are valid instantiations for Date here.
However, the date strings used for date1 and date2 in the example are not valid, therefore not supported in all browsers.
Instead of '2014-03-25 07:30 AM' (which is invalid) you should do one of the following:
convert your input into milliseconds
convert your input into date string, like "October 13, 1975 11:13:00"
calculate the year, month, day, hours, minutes, seconds and milliseconds, then pass them to the instantiation
I have a string 10/11/2012 meaning November 10, 2012.
But when I do new Date("10/11/2012") it returns October 11th.
How do I pass in the date format I want? In this case dd-mm-yyyy
I found jQuery.datepicker.parseDate(format, Date) at this site:
http://docs.jquery.com/UI/Datepicker/$.datepicker.parseDate
So I will be using the jQuery datepicker instead.
Unfortunately, there's no JavaScript Date constructor that allows you to pass in culture information so that it uses localized date formats. Your best bet is to use the constructor that takes the year, month, and day separately:
var parts = dateString.split('/');
var date = new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10),
parseInt(parts[0], 10));
For this specific case, you can use:
var dateparts = date.split("/");
var datestring = dateparts[1] + "/" + dateparts[0] + "/" + dateparts[2];
var date = new Date(datestring);
In the more general case, you can extend the Date prototype, as demonstrated in this answer:
https://stackoverflow.com/a/13163314/1726343
Any idea why this function doesn't work properly in Internet Explorer?
function days_between(check_in, check_out)
{
var oneDay = 24*60*60*1000;
var firstDate = new Date(check_in);
var secondDate = new Date(check_out);
var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
return diffDays;
}
in internet explorer it shows NaN as result.
im calling this function in this date format
var check_in = "2012-02-09";
var check_out = "2012-02-12";
var range = days_between(check_in, check_out);
Regards
IE doesn't support Date.parse or passing "2012-02-09" (with ISO dates) to new Date, you need to parse it yourself and pass new Date( 2012, 1, 9 ) or use a Date.parse shim for ISO dates
The date format you're passing (yyyy-mm-dd) isn't supported by Date. See the note here that says it must be in a format parsable by parse. See here for acceptable parse formats: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse
You have problem in creating the Date Object
Date objects are created with the Date() constructor.
There are four ways of instantiating a date:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Most parameters above are optional. Not specifying, causes 0 to be passed in.
Once a Date object is created, a number of methods allow you to operate on it. Most methods allow you to get and set the year, month, day, hour, minute, second, and milliseconds of the object, using either local time or UTC (universal, or GMT) time.
All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.
Some examples of instantiating a date:
var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)
(Taken from http://www.w3schools.com/js/js_obj_date.asp)
You are giving the date arguments in an incorrect format. You can expect javascript to support these formats:
MM-dd-yyyy
yyyy/MM/dd
MM/dd/yyyy
MMMM dd, yyyy
MMM dd, yyyy
To fix your immediate problem, you can use replace() to format your arguments.
function days_between(check_in, check_out)
{
var firstDate = new Date(check_in.replace('-' , '/'));
var secondDate = new Date(check_out.replace('-' , '/'));
var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / 86400000);
return diffDays;
}
And by the way, you can replace oneDay with a constant.