How to fetch date from date function in javascript - javascript

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

Related

javascript create date from string and cast back to string

I am creating a date out of strings. I want my date to have the last hour and last minute of the given day. I have a calendar that is populating an input box with a date in the form 01-02-2020 (dd-mm-yyyy).
I want to add hours minutes and seconds to the date to make it look like this string: 2020-02-01 23:59:59.
I then want to sabtract x number of days from the date I've created to get a startdate.
My issue is that my date values are being somehow converted when I use the date functions. What I am doing is:
enddate = new Date(enddate);
enddate = enddate.setHours(23,59,59);
var startdate = new Date();
startdate.setDate( enddate.getDate() - 5);
I then want to concatenate my two dates to a string. Like ?startdate=2020-01-26 00:00:00&enddate=2020-02-01 23:59:59. Where the startdate has hours, minutes and seconds in the form 00:00:00 This string is what I ultimately want and it doesn't matter how I get to this start and enddate value. The steps above are just what I've tried. And actually the format of my dates in the the final string doesn't matter as long as it is something that sql can recognize and treat as a date.
How can I accomplish this?
Here is my full code:
enddate holds a date value in this form: 01-02-2020 (day month year european style)
datesplit = enddate.split("-");
enddate = new Date(datesplit[2],datesplit[0],datesplit[1]); //mm-dd-yyyy for US format
enddate.setHours(23);
enddate.setMinutes(59);
var startdate = new Date(enddate);
startdate.setDate(startdate.getDate() - daysback);
startdate.setHours(00);
startdate.setMinutes(00);
console.log("startdate and enddate: " + startdate + " - " + enddate)
//startdate and enddate: Tue Jan 28 2020 00:00:00 GMT-0500 (Eastern Standard Time) - Sun Feb 02 2020 23:59:00 GMT-0500 (Eastern Standard Time)
console.log("startdate and enddate date string: " + startdate.toISOString() + " - " + enddate.toISOString());
//startdate and enddate date string: 2020-01-27T05:00:00.000Z - 2020-02-03T04:59:00.000Z
Why the added time in the last console log value when the date is cast to ISO?? That last format is what I want, but value is different.
var st = "01-02-2020";
var pattern = /(\d{2})\-(\d{2})\-(\d{4})/;
var dt = new Date(st.replace(pattern,'$3-$2-$1'));
enddate = new Date(dt);
enddate.setHours(23,59,59);
var startdate = new Date(dt);
startdate.setHours(00,00,00);
startdate.setDate( startdate.getDate() - 5);
function js_yyyy_mm_dd_hh_mm_ss (mydate) {
now = new Date();
year = "" + mydate.getFullYear();
month = "" + (mydate.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
day = "" + mydate.getDate(); if (day.length == 1) { day = "0" + day; }
hour = "" + mydate.getHours(); if (hour.length == 1) { hour = "0" + hour; }
minute = "" + mydate.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
second = "" + mydate.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
var query = '?startdate='+js_yyyy_mm_dd_hh_mm_ss(startdate)+'&enddate='+js_yyyy_mm_dd_hh_mm_ss(enddate);
alert(query);
Well the problem basically was that you assign the same variable to different uses. I separate it (by adding 1 to the second one) and that solve it:
const enddate = '02-01-2020';
const daysback = 5;
datesplit = enddate.split("-");
enddate1 = new Date(datesplit[2],datesplit[0],datesplit[1]); //mm-dd-yyyy for US format
enddate1.setHours(23);
enddate1.setMinutes(59);
var startdate = new Date(enddate1);
startdate.setDate(startdate.getDate() - daysback);
startdate.setHours(00);
startdate.setMinutes(00);
console.log("startdate and enddate: " + startdate + " - " + enddate)
//startdate and enddate: Tue Jan 28 2020 00:00:00 GMT-0500 (Eastern Standard Time) - Sun Feb 02 2020 23:59:00 GMT-0500 (Eastern Standard Time)
console.log("startdate and enddate date string: " + startdate.toISOString() + " - " + enddate1.toISOString());
//startdate and enddate date string: 2020-01-27T05:00:00.000Z - 2020-02-03T04:59:00.000Z

Javascript convert string to date format yyyy-MM-dd

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

Convert a date in javascript

I need to convert the string date format from
Fri Feb 20 11:13:43 GMT 2015 to
2015/20/02 11:12
I used
var dateTest = new Date("Fri Feb 20 11:13:43 GMT 2015");
var yr = dateTest.getYear();
but yr seems to return "115"
Use
var yr = DateTest.getFullYear();
You want to use
var yr = dateTest.getFullYear();
Try This, Its Return full date and you can also change its format as you want
For Return Year
var d = new Date();
alert(d.getFullYear());
Return Full Date
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var output = (day < 10 ? '0' : '') + day + '/' +
(month < 10 ? '0' : '') + month + '/' +
d.getFullYear();
alert(output);
Here is Fiddle

how to convert date object of js to time in h:i A (7:30 A.M) format

I have a javascript object of date and its value is:
Thu Dec 18 2014 07:29:44 GMT+0500 (PKT)
Now I want to get the time from above data object in following format:
7:29 A.M
How can I do that using javascript code.
Please Help!!
You can do a little string addition by doing the following:
var result = "";
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var amORpm = hours > 12 ? "P.M" : "A.M";
if(hours > 12) hours -= 12;
result = hours + ":" + minutes + " " + amORpm;
console.log(result); // Will get you your desired format

Parsing date and time from formatted date

I have a date in the following format:
Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)
Can anyone tell me how to parse the date
Wed Jul 17 2013
and time out of this without using string functions
1:00pm
One of the simplest ways is to make sure your LOCALES are set properly, which allows you to do something like this:
var myStrDate = 'Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)';
var myDate = Date.parse(myStrDate);
var myDateOnly = myDate.toLocaleDateString();
var myTimeOnly = myDate.toLocaleTimeString();
Here's a great place to start: http://www.w3schools.com/jsref/jsref_obj_date.asp
You can construct a Date object with the date string, combined with additional functions to get the required output. dayname() returns the dayname, monthname() returns the three-letter month name.
I've used console.info() for outputting results - alert() can be used as an alternative if appropriate.
function dayname(d)
{
var names = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri","Sat" ];
return names[d.getDay()];
}
function monthname(d)
{
var names = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
return names[d.getMonth()];
}
// construct new date object passing date string as parameter
var d = new Date("Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)");
console.info(dayname(d) + ' ' + monthname(d) + ' ' + d.getDate() + ' ' + d.getFullYear());
For the time, this gettime() function checks the hour to output am or pm correctly, and for correct formatting-:
function gettime(d)
{
var ampm = "am";
var hour = d.getHours();
var mins = d.getMinutes();
if (hour >= 12)
{
hour = hour - 12;
ampm = "pm";
}
if (hours < 10)
hours = "0" + hours;
if (mins < 10)
mins = "0" + mins;
return hour + ":" + mins + ampm;
}
console.info(gettime(d));
var ms = Date.parse(new Date("Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)"));
var curr_date = ms.getDate();
var curr_month = ms.getMonth() + 1; //Months are zero based
var curr_year = ms.getFullYear();
var hours = ms.getHours();
var mins = ms.getMinutes();
suf = (hours >= 12)? 'pm' : 'am';
hours = hours % 12;
document.write(curr_date + "-" + curr_month + "-" + curr_year + " " + hours + ":" + min + " " + suffix);
Also try using moment.js, you can format dateTime as you wish.
Here great reference to learn date functions in javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Categories

Resources