Parsing MySQL date in JavaScript - javascript

I want to display the date timestamp retrieved from a MySQL database in a HTML table dynamically. I have an array of dates. I am getting date in the following format:
Mar 10, 2014 6:40:45 AM
How can I get the date as it is and represent it in my HTML table using JavaScript?

Assuming that Mar 10, 2014 6:40:45 AM is your input date format, This code will help:
var myDate = new Date('Mar 10, 2014 6:40:45 AM');
var reqDate = ((myDate.getMonth() + 1) + "/" + myDate.getDate() + "/" + myDate.getFullYear());
console.log(reqDate);
output
3/10/2014

Given your clarification that you cannot change the format of the incoming date, you need something like this:
var dateParts = isoFormatDateString.split("-");
var jsDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2].substr(0,2));

Related

New date() returning next date instead of today's date

i am trying to convert a string into date type.i am giving the string value to new date().
but it's returning next day date instead of date which i am trying to convert.
var endDate = new Date("2017-03-23T23:59:59.000Z");
//end date value is now ------ Fri Mar 24 2017 05:29:59 GMT+0530 (India Standard Time).
Please suggest me how can get correct date in the format MM/DD/YYYY
This hack can help you,
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
it will give you,
"2017-03-23T23:59:59.000Z"
Further if you want to convert it to DD/MM/YYYY then you can use native javascript or lib like moment for that,
This simpile js will help to convert it to any format.
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
var d1 = endDate.split('T'); //spliting date from T
var d2 = d1[0].split('-'); //getting date part
console.log('yyyy/MM/dd', d2[0] + "/" + d2[1] + "/" + d2[2]) //YYYY/MM/DD
console.log("DD/MM/YYYY", d2[2] + "/" + d2[1] + "/" + d2[0])
jsfiddle link
if your time is in IST use below
var endDate = new Date("2017-03-23T23:59:59.00+0530");
If you check dates, you will see that your dates differs in 5h 30 mins, that is same as your date saying GMT +0530. Your original date has .000Z that is time zone of GMT +0.
Make sure you use same time zone when working with date.
Try using Date.UTC('your date')
JavaScript Date objects carry no timezone information. The only reason you saw a non-UTC date is that the browser chooses by default to display dates as local time in the console. If you don't care about the date object aligning with the exact instant in local time, you can use the following format function to turn it into MM/DD/YYYY format:
function format (date) {
var mm = ('0' + (date.getUTCMonth() + 1)).slice(-2)
var dd = ('0' + date.getUTCDate()).slice(-2)
var yyyy = date.getUTCFullYear()
return mm + '/' + dd + '/' + yyyy
}
var endDate = new Date("2017-03-23T23:59:59.000Z")
console.log(endDate.toISOString())
console.log(format(endDate))
(Credit to Ranj for posting an answer using Date#toISOString before mine.)
I have created the solution over here please find below link
https://www.w3schools.com/code/tryit.asp?filename=FD0YSGRMB59W

JQuery format value for date

I am updated the value of an HTML input field with data from a query:
$("##warranty_end_date#id#").val(warranty_end_date);
But the data is store in my database as a SQL Date/Time and has an output like this: May, 08 2019 00:00:00 -0400
I would like the data to be formatted like this: 05/08/2016
How can I accomplish this?
warranty_end_date = "May, 08 2019 00:00:00 -0400";
var d = new Date(warranty_end_date);
var f = ("00" + (d.getDate()).toString()).slice(-2) + "/" + ("00" + (d.getMonth()+1).toString()).slice(-2) + "/" + (1900 + d.getYear()).toString();
$("##warranty_end_date#id#").val(f);
Maybe you could format the date in the SQL query already.
Something like:
SELECT convert(varchar, getdate(), 101)
101 = mm/dd/yyyy – 10/02/2008
var date = new Date('2010-10-11T00:00:00+05:30');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
You can accomplish this using the following:
var date = new Date('May, 08 2019 00:00:00 -0400');
var output = date.toLocaleFormat('%m/%d/%Y');
alert(output);
Here's a jsfiddle: https://jsfiddle.net/y4zemzqg/
Also, look over using moment.js. http://momentjs.com/ Moment.js makes formatting dates and time incredibly easy. I've used it for quite some time with no issue:
var theDate = moment('May, 08 2019 00:00:00 -0400').format('MM/DD/YYYY');
alert(theDate);
Here is a JS Fiddle using the moment.js solution: https://jsfiddle.net/v923bn5s/

Convert Unix time stamp to readable format not working in JavaScript code

I was trying to convert a timestamp to a format(July 14, 2016) and I used the following code:
var wiDaterawa = entry.data.channel.created;
var wiDateraw = new Date(entry.data.channel.created * 1000);
var months =
['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = wiDateraw.getFullYear();
var month = months[wiDateraw.getMonth()];
var date = wiDateraw.getDate();
var wiDate = month + ' ' + date + ', ' + year ;
By this code I tried to pass the timestamp: 1481791797000 and got the result as:
Feb 11, 48926
The year is not showing in proper manner.
When I tested it with:
var wiDate = new Date(entry.data.channel.created);
It showed :
Thu Dec 15 2016 12:49:57 GMT+0400 (Arabian Standard Time)
Also I tried it with php code:
<?php print $returnValue = date('M d,Y', 1481791797000); ?>
Got the result:
Feb 11, 48926
I need the JavaScript based result as I am binding all the result in the a JavaScript function.
Could you please help me to sort it out?
You should use a library like moment.js
http://momentjs.com/

JavaScript date format convert.

Date from datetimepicker have format:
var currentDate = new Date(); currentDate = Thu Jul 14 2016 09:10:04 GMT+0200 (Środkowoeuropejski czas letni) {}
And this date have methods like .getFullYear() etc.
But when I send it to my API where this date is DateTime and send back it to frontend it look that 2016-07-22T22:00:00Z and it doesn't have methods like .getFullYear() etc.
That is problem for me. I need detect if the date is formatted yyyy-mm-ddThh-mm-ssZ and convert it to the first format.
How I can do it? I can't use momentjs.
format your date string in javascript
var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var year = date.getFullYear();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var datetime= day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;
Then in c#
DateTime.ParseExact(DatetimeString , "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Why you do not use this:
http://momentjs.com/
this allows all sorts if formatting and its safer to use due to incorrect dates possible
var a = moment('2016-01-01');
var b = a.add(1, 'week');
a.format();
"2016-01-08T00:00:00-06:00"
moment().format("MMM Do YY"); // Jul 14th 16
Use standard format yyyy-mm-dd hh-mm-ss
You're possibly receiving a string from your API. So you have to parse it back to Date object
var newDateObject = new Date(dateStringFromAPI);
Then you can access newDateObject.getDay() newDateObject.getFullYear() etc
as you are using AngularJS you can simply format date with Angular's date filter https://docs.angularjs.org/api/ng/filter/date
Good day to you!
Looks like the front-end doesn't parse the API-dataset to create a date-object (no date-object—no date-methods)—here is a solution:
Enable date-parsing on the front-end;
If needed, apply a date-time format on API output (MSDN-articles: Date and Time Format Strings—Standard and Custom);
Send the API-processed data to the front-end.

Creating a Javascript Date object by passing in a date? What is a dateString?

I am trying to figure out what the W3C website means by dateString.
http://www.w3schools.com/jsref/jsref_obj_date.asp
I am trying to do something like:
var _date = new Date("Mon Aug 12 2013 2:00 AM");
or even:
var _date = new Date("Mon, Aug 12 2013, 2:00 AM");
Is there a quick way of turning my string into a format that the date object likes?
Thank you
edit:
I suppose it expects the following:
var d = new Date()
d.toDateString()
"Tue Aug 13 2013"
Is it only that type of string?
The Javascript string-based Date constructor accepts strings in a format accepted by Date.parse().
These are date strings compliant with RFC-2822 or ISO-8601.
Use String in this format:
new Date('2013-08-13')
or
new Date('2013-08-13T10:51:00');
Here, this is how to use dateString as a parameter
var dateString = "08/12/2013";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
Remember month are stored at 0th index in javascript.

Categories

Resources