JavaScript: Convert date string mm/dd/yyyy to date mm/dd/yyyy - javascript

I receive a string that comes in from SQLserver with the format:
'mm/dd/yyyy' or CONVERT(VARCHAR(10), [ActivityDate], 101)
I need to convert that string value, to an actual date value, but keeping the same date format:
mm/dd/yyyy
I need to format the date is because it comes from SQL server in format '2015-02-18 00:00:00.000' to a page that uses angularJS sort and fileter taken from this example: https://scotch.io/tutorials/sort-and-filter-a-table-using-angular
in my table, I have a date column that uses format mm/dd/yyyy, when I type 12/21/2015 I get nothing from the filter even though there are records with this date. The reason why the filter does not work, is because the date even thouhg it displays as mm/dd/yyyy, still has the fromat from sql. The filter works when I type the date 2015-12-21, but that would be misleading to the user.
Does this makes sense?

For your case, you can use new Date() constructor that implicitly calls Date.parse()
new Date('02/21/1994')
//> Date 1994-02-20T21:00:00.000Z
+1 to Claies, i recommend to use moment.js too

Related

How can I check month and day order in mixed date format?

There's column which contains date in different formats (ie. YYYY-MM-DD or YYYY-DD-MM).
When I query it with format time:date, it throw an error: date/time field value out of range: "2022-23-02"
How can i solve it?
How can I check if it's YYYY-MM-DD or YYYY-DD-MM or another?
If you have no field that tells you whether it's YYYY-DD-MM or YYYY-MM-DD, you are kinda out of luck because it's possible that a value could be valid in both formats.
If you are able to redesign, and you must store the date as a string then use YYYY-MM-DD as it's easier for sorting. Optimally, just pass a JavaScript Date object to field of type date, timestamp, or timestampz to the database driver or orm and let it handle the conversion for you. Here's an example with node-postgres: https://node-postgres.com/features/types (the section labeled "date / timestamp / timestamptz")

How do I format a date as ISO date(mongodb) using moment.js?

I'm trying to convert below-mentioned date to ISO format(MongoDB)
var d = { storedDate: '26/06/2020 05:55:29 PM' };
I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried the below piece of code.
moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");
How can I get it as ISODate("2020-06-26T17:55:29.274Z")
Please advice
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toISOString() will return ISO date only in UTC that you need for MongoDb. You need to provide the input date format as well.
If you want to store proper Date object, use
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()
You should not store date/time values as strings, use proper data type.

What kind of format is this date in?

2018-06-04T01:00:45.500Z
Moment.js returns a date object when calling moment(), but sometimes returns this timestamp.
How can I make sure I'm receiving a timestamp like this and not a date object?
For example, output of calling console.log(moment()):
Did you try this?
moment().format('YYYY-MM-DDTHH:mm:ssZ');
This returns an output in this format :
2018-06-04T12:49:53+10:00
That format is ISO, is the universal format to represent a date in javascript. It's the same format that you get from Date.toISOString()
You can get this date format as follow
console.log(moment().toISOString())
reference https://momentjs.com/docs/#/displaying/as-iso-string/

Convert database date to short date with Javascript

I have a SQL lookup a date that feeds into a field, but the date format contains time, I need to convert it to short date (mm/dd/yyyy). The MSSQL outputs this format (m/d/yyyy 12:00:00 AM) notice that the time is always '12:00:00 AM'. How do I remove the time?
$('#q60').change(function () {
var date = $('#q60 input').val(); //looking up the field that contains the date fed from SQL.
date = date.substring(0, date.indexOf(' '));
});
I have tried using split but while it output the correct thing it doesn't actually change the value in the field for some reason. I have also attempted using the .format similar to this post: Format a date string in javascript
But I am stuck!
with date = date.substring(0, date.indexOf(' ')); you're just storing splitted value in to date variable. to change the value of the input field add $('#q60 input').val(date) at the end of your function.
also in JS there's a whole Date object, with it you can format your date as you please. you can find more about it here and here

.NET WebService JSON date in ISO-8601 format

I am calling a .net asmx webservice that returns a number of fields. One of the fields in a date. The date is in the format of: "effective_date":"\/Date(978411600000)\/"
According to this SO question: How do I format a Microsoft JSON date? it would be better if the date returned was in ISO 8601 format, this way JavaScript would be able to interpret it as a date.
Currently I use the following javascript: new Date(d.effective_date) and I get the message Invalid Date. According to the linked SO question I should be able to do this if I can get the web service to pass the date in ISO format rather than in \/Date(978411600000)\/ format.
My question is, how do I get the webservice to return the date in ISO 8601 format?
Note:
I'm aware that I can use this (per the answer from the linked question): var date = new Date(parseInt(d.effective_date.substr(6)));, however it is mentioned in a comment that Incoming date values should be formatted in ISO-8601, so I'm wondering how to get the incoming date from the web service to be in this ISO format.
You may use:
var date = new Date(d.effective_date);
date.toISOString(); // ISO-8601 formatted string
JSFiddle: http://jsfiddle.net/nanndoj/gjtkvrsy/

Categories

Resources