Convert a string from laravel controller to javascript - javascript

I would like to convert a string value that I fetch from my database and convert it to a date variable in the view using javascript
I have tried out the following code;
var date = moment({!! $cheque->cheque_date !!}).format('YYYY-MM-DD');
I am getting the following result when I console log the date variable.
1970-01-01

you need to add " quotations
checkout this
var date = moment("{{ $cheque->cheque_date }}").format('YYYY-MM-DD');
this will converted to
var date = moment("2019-04-10").format('YYYY-MM-DD');
while your code will be converted to
var date = moment(2019-04-10).format('YYYY-MM-DD'); // no quotations

Related

How to convert a Timestamp into MySQL DateTime in JavaScript?

I have a timestamp format which is just like below which is an int.
1631514003973
I am passing this value to an API which is built with Node.JS. How can I convert this into Mysql DateTime format?
I checked this answer but it is about getting the current date and not converting a timestamp. I am coming from a Java background so this is bit confusing for me.
You can pass the Date object of JavaScript directly to MySQL. And MySQL will automatically generate the DateTime format from that Date object.
const date = new Date(1631514003973);
I assume that integer represents number in milliseconds since the Epoch time. If that is the case try this code:
const numberOfMs = 1631514003973;
const epochDate = new Date(1970,1,1);
const myDate = new Date(epochDate .getTime() + numberOfMs);
For that you can use either in nodejs layer. You need to convert the timestamp to date using below and use in the mysql query.
const date = new Date(1631514003973);
console.log(date)
For the use direct in mysql you can use mysql date function and
SELECT DATE_FORMAT(FROM_UNIXTIME(DATE(1631514003973)), '%e %b %Y') AS 'date_formatted' FROM table
we need to create a new function using JavaScript.
<script>
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log("Date: "+date.getDate()+
"/"+(date.getMonth()+1)+
"/"+date.getFullYear()+
" "+date.getHours()+
":"+date.getMinutes()+
":"+date.getSeconds());
</script>
Output:
Date: 4/12/2020 19:34:25
If you want only date (MM/DD/YYYY), you should fallow this:
var timestamp=1370001284;
var todate=new Date(timestamp).getDate();
var tomonth=new Date(timestamp).getMonth()+1;
var toyear=new Date(timestamp).getFullYear();
var original_date=tomonth+'/'+todate+'/'+toyear;
console.log(original_date);

How to format dates with Pentaho Spoon

How do I convert the string 03-MAR-2021 to the string 20210303 with Javascript in Pentaho Spoon
start_date="03-MAR-21";
var new_startDate= new Date(start_date);
var date= moment(new_startDate).format('yyyyMMdd');
See common date formats
start_date="03-MAR-2021";
var date= str2date(start_date, "dd-MMM-yyyy");
var formatedDateString = date2str(date, "yyyyMMdd");
I get the date in a string like '03-Mar-2021' but I need to convert to string '20210303' (YYYYMMDD) I can make in JScript or directly on Query. But I have problems when I try to

Extract epoch from moment object

I am trying to extract the unix timestamp (epoch) from a date string as follows:
var week = $("#week").val();
var timestamp = moment(week).format("X");
console.log(timestamp);
This returns, in the console "Invalid Date".
I am passing the following format: "03-Nov-16"
I am trying to return the unix timestamp of a date.
Any help on this would be appreciated.
You need to tell moment the format of the date string you're trying to parse. I think you're looking for something like this (not tested):
var timestamp = moment("03-Nov-16", "DD-MMM-YY").unix()
See the official docs on date string parsing for more info.

C# DateTime to Javascript parse returns

on the aspx I am getting
date = /Date(1420460565000)/
I tried to parse it javascript date bject
var dateformatted = new Date(date);
However when I run it I am getting Invalid Data
How do I parse the c# DateTime object?
You could try this one:
var dateformatted = new Date(parseInt(date.substr(6)));
This works because substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor. Hence a new Date can be created.

Appcelerator Titanium JS dosen't parse Date() as expected

I am using Appcelerator Titanium, and I'm trying to parse a date string as a new Date object and then use the .getTime() function but it keeps returning "NaN"
var d = new Date("2014-02-01T00:00:00");
var time = d.getTime();
console.log(time); // returns NaN
Am I doing anything wrong here? It works when I create a new date for now, like this:
var d = new Date();
var time = d.getTime();
console.log(time); // returns correct value
I can't see why the first example is working but the second example is not.
You're trying to parse a UTC date time. In Titanium, when you try to parse the date, it will return invalid date. So you need to convert it to datetime string. You can use Convert UTC Date to datetime string Titanium to convert the time.

Categories

Resources