I have a string which is in the format "05-01-2016" when i run the below code in chrome i get the correct output
var fromDate = new Date(searchOpts.RunDateFrom);
fromDate.format("yyyy-MM-dd");
output = "2016/05/01"
However, when this code is execute inside my js file i get this output
Sun May 01 2016 00:00:00 GMT+0530 (India Standard Time)
How do i prevent this? I need to pass the date in this format "2016-05-01" to solr
formattedDate = fromDate.getFullYear() + "-" + (fromDate.getMonth()+1) + "-" + fromDate.getDate()
If you just need the string
var year = fromDate.getFullYear();
var month = fromDate.getMonth() < 10 ? '0'+ fromDate.getMonth()+1 : fromDate.getMonth()+1
var date = fromDate.getDate() < 10 ? '0'+ fromDate.getDate(): fromDate.getDate()
Related
we stuck date comparison, we tried the below code. EndDate value is related to jan 21, 2016 but in alert showing showing Fri Sep 01 2017 10:10:10 GMT+0530 (India Standard Time), could you please share me your ideas
var endDateVal = "21/01/2016 10:10:10".replace(/-/gi, "/");
alert(new Date(endDateVal));
if (new Date(endDateVal) > new Date()) {
alert("Last end date should be minor than today");
}
Fiddle
The date is formatted incorrectly. The date needs to be an ISO 8601 or IETF-compliant RFC 2822 formatted date, like:
2016-01-21T10:10:10+05:30
To format the date you have you could do some thing like:
var dateVars = "21/01/2016 10:10:10".match(/\d+/g);
var reformattedDate = dateVars[2] + '-' + dateVars[1] + '-' + dateVars[0] + 'T' + dateVars[3] + ':' + dateVars[4] + ':' + dateVars[5] + '+05:30';
new Date(reformattedDate);
First, new Date("21/01/2016 10:10:10") returns Invalid Date. Default format for Date is mm/dd/yyyy and not dd/mm/yyyy.
Second, when comparing dates, you should use date.getTime() instead.
Following is a sample code.
var endDateVal = "02/21/2016 10:10:10";
var d1 = new Date(endDateVal)
var d2 = new Date();
console.log(d1, d2)
if (+d1 > +d2) {
alert("Last end date should be minor than today");
}
You need to swap your day/month around to 01/21/2016 10:10:10.
Also, Im not sure why you are using .replace(/-/gi, "/"); as this is replacing a - with / where your date does not have any -.
I need some help with converting unixtime to a specific format. Here is what I am currently working with:
var date = "2014-05-01";
var indexPie = Date.parse(date);
I need indexPie in yyyy-mm-dd format. What I do not understand is that when log
var newDate = new Date(indexPie);
The results is:
Wed Apr 30 2014 18:00:00 GMT-0600 (Mountain Daylight Time)
when it should be:
Thur May 01 2014 18:00:00 GMT-0600 (Mountain Daylight Time)
Why is new Date(indexPie) resulting in Apr 30 and how do I get my correct format of yyyy-mm-dd?
Any suggestions would be great. Thanks.
I resolved the issue with the following:
var date = new Date(indexPie);
var year = date.getUTCFullYear();
var month = date.getUTCMonth() + 1;
var day = date.getUTCDate();
var dateString = year + "-" + month + "-" + day;
You are expecting that the value in date variable: "2014-05-01" will be parsed as in local timezone, but actually it is parsed as in UTC.
You can convert the date from UTC to local timezone like this:
var newDate = new Date(indexPie + new Date().getTimezoneOffset() * 60000);
I want to add month into the select date by the user.
startdate=document.getElementById("jscal_field_coverstartdate").value;
now I want to add 11 month from the above startdate. How to do that.
date format = 2013-12-01
Without the date format it is difficult to tell, however you can try like this
add11Months = function (date) {
var splitDate = date.split("-");
var newDate = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
newDate.setMonth(newDate.getMonth() + 11);
splitDate[2] = newDate.getDate();
splitDate[1] = newDate.getMonth() + 1;
splitDate[0] = newDate.getFullYear();
return startdate = splitDate.join("-");
}
var startdate = add11Months("2013-12-01");
alert(startdate)
JSFiddle
If your startdate is in correct date format you can try using moment.js or Date object in javascript.
In Javascript, it can be achieved as follow:
var date = new Date("2013-12-01");
console.log(date);
//output: Sun Dec 01 2013 05:30:00 GMT+0530 (India Standard Time)
var newdate = date.setDate(date.getDate()+(11*30));
console.log(new Date(newdate));
// output: Mon Oct 27 2014 05:30:00 GMT+0530 (India Standard Time)
In above lines, I have used 30 days per month as default. So you will get exact 11 month but little deviation in date. Is this what you want ? You can play around this likewise. I hope it help :)
For more about Date you can visit to MDN.
You can do it like this:
var noOfMonths = 11
var startdate = document.getElementById("jscal_field_coverstartdate").value;
startdate.setMonth(startdate.getMonth() + noOfMonths)
Try this:
baseDate.setMonth(2);
baseDate.setDate(30);
noMonths = 11;
var sum = new Date(new Date(baseDate.getTime()).setMonth(baseDate.getMonth() + noMonths);
if (sum.getDate() < baseDate.getDate()) { sum.setDate(0); }
var m = newDate.getDate();
var d = newDate.getMonth() + 1;
var yyyy = newDate.getFullYear();
return (yyyy+"-"+m+"-"+d);
Notes:
Adding months (like adding one month to January 31st) can overflow the days field and cause the month to increment (in this case you get a date in March). If you want to add months and then overflow the date then .setMonth(base_date.getMonth()+noMonths) works but that's rarely what people think of when they talk about incrementing months.
It handles cases where 29, 30 or 31 turned into 1, 2, or 3 by eliminating the overflow
Day of Month is NOT zero-indexed so .setDate(0) is last day of prior month.
I need the current date so I am writing a below code
dateCreated = new Date();
which returns Thu Dec 05 2013 12:57:48 GMT+0530 (India Standard Time).
But when I am trying to store it into database it is showing an error
I am using DateTime datatype to store date in SQL Server 2012 database
I even tried using Date datatype but it is showing me error.
Is there any way to do this?
I just want to store date
You need to put it in a format that SQL understands. It doesn't understand the string Thu Dec 05 2013 12:57:48 GMT+0530 (India Standard Time) just format it how SQL likes it.
e.g. YYYY-mm-dd
Format the date according your need
function getDate() {
TDate = new Date();
CurYear = TDate.getYear();
CurMonth = TDate.getMonth();
CurDay = TDate.getDate();
CurHour = TDate.getHours();
CurMins = TDate.getMinutes();
CurSecs = TDate.getSeconds();
CurDay = ((CurDay < 10) ? '-0' : '-') + CurDay;
CurMins = ((CurMins < 10) ? ':0' : ':') + CurMins;
CurSecs = ((CurSecs < 10) ? ':0' : ':') + CurSecs;
TheDate = '';
TheDate += ((CurYear%1900)+1900) + '-';
TheDate += CurMonth;
TheDate += CurDay + ' ';
TheDate += CurHour;
TheDate += CurMins;
TheDate += CurSecs;
return TheDate;
}
alert(getDate()); //YYYY-MM-DD HH:MM:SS
I have the following date format: 2010-04-15 23:59:59
How would I go about converting this into: 15th Apr 2010
using javascript
Check out Datejs. You'll probably want to parse your timestamp and then call toString('MMS MMM yyyy') (or something to that effect) on it. Here's some more info on toString.
There's probably a more-lightweight solution, if this is all you're after.
Like dylanfm suggests, I would check out Datejs, but if you only want to convert the way you are describing, this might work for you:
var m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var p = ["th","st","nd","rd"];
// create date object - have to replace dashes with slashes
var d = new Date("2010-04-15 23:59:59".replace(/-/g,"/"));
// index in array p based on date % 10
var pn = d.getDate() % 10;
// pick "th" for days 11-13
if (d.getDate() > 10 && d.getDate() < 14 ) pn = 0;
// pick "th" for days 4-9, 14-19, 24-29
if (pn >= p.length) pn = 0;
// date in format "15th Apr 2010"
var formatted = d.getDate() + p[pn] + " " + m[d.getMonth()] + " " + d.getFullYear();
Note that this is nowhere near as flexible as using a library like Datejs.