Convert string to date format in JavaScript - javascript

I have a string variable I want the variable into date format.
My Code:
var date = '2/3/2022 17:57:30'
var temp = new Date(date)
My Output:
2022-02-03T17:57:30.000Z
Expected Output:
2022-02-03

You can use toLocaleDateString with an argument that specifies a locale for which your format is used. For instance, the Swedisch locale uses the YYYY-MM-DD format:
var date = '2/3/2022 17:57:30'
var temp = new Date(date).toLocaleDateString("se");
console.log(temp);

There's no built-in method to convert date into your expected output although you can try this piece of code to convert date into your expected output;
var date = '2/3/2022 17:57:30'
var temp = new Date(date).toLocaleDateString().replaceAll("/","-");

Related

How do I convert date mm/dd/yyyy to yyyy-mm-ddT00:00:00.000Z format in javascript

var dateType = $filter('date')($scope.datalist[0].OVERRIDE_DATE, "MM/dd/yyyy");
I have the output : 05/07/1992
I want to change above date to yyyy-mm-ddT00:00:00.000Z format ?
toISOString() should do what you want.
e.g.
new Date('05/07/1992').toISOString();
will give you
"1992-05-07T07:00:00.000Z"
The Date.prototype.toISOString() method may be what you want:
const rawDate = $filter('date')($scope.datalist[0].OVERRIDE_DATE, "MM/dd/yyyy");
const formattedDate = new Date(rawDate).toISOString();
or, if $scope.datalist[0].OVERRIDE_DATE is already a date object, you could just do:
const formattedDate = $scope.datalist[0].OVERRIDE_DATE.toISOString();
Link to Date.prototype.toISOString docs

Moment or without, input a string date 'dd/MM/yyyy' and would like a ISO string in output

I tried followed many articles, but the final ISO string is always with the date wrong:
input: 15/03/2018
output: basing on my timezone, an ISO String
c.data.due_date_date = moment(c.data.due_date, "DD-MM-YYYY").toDate();
console.log('ADD TASK toDate', c.data.due_date_date);
c.data.due_date_date = c.data.due_date_date.toDateString();
console.log('ADD TASK toISOString', c.data.due_date_date);
in this case the output is 018-03-14T23:00:00.000Z
Any idea?
You can try following this worked for me.
moment('inputDate','format').toISOString()
Ex:
moment('15/03/2018','DD-MM-YYYY').toISOString()
This way you can achieve it:
var str = "28/12/1994";
var dateStr = str.split("/").reverse().join("-");
var d = new Date(dateStr);
var isoString = d.toISOString();
console.log(isoString); //prints 1994-12-28T00:00:00.000Z

Parse dates from Javascript Date Object to regular datetime

How do I convert a date from JS Date Object to php datetime?
The following dates were converted to a Javascript Date Object from php datetimes:
"startsAt": "2015-08-15T10:46:00+00:00",
"endsAt": "2015-08-15T11:46:00+00:00"
converted using this syntax in javascript:
for (var i = 0; i < events.events.length; i++) {
events.events[i].startsAt = new Date(events.events[i].startsAt);
events.events[i].endsAt = new Date(events.events[i].endsAt);
}
startsAt and EndsAt looked like so after the conversion:
startsAt: Date 2015-08-15T11:46:00.000Z
endsAt: Date 2015-08-15T11:46:00.000Z
Now my goal is to do the opposite in javascript (angularjs): Convert from Javascript Date Object to php datetime 2015-08-15T10:46:00+00:00. Any idea would help. Thanks
Just to fix correct an answer below:
var rawDate = new Date();
var parsedDate = [
rawDate.getDate(),
(rawDate.getMonth() + 1), //because getMonth() starts from 0
rawDate.getFullYear()
].join('-');
console.log(parsedDate); // output could be: 19-08-2015 for example
or just call any of the following to convert date object to string:
rawDate.toJSON();
rawDate.toISOString();
You've got to parse it manually doing something like:
var rawDate = new Date();
var parsedDate = rawDate.getDate() + '-' + rawDate.getMonth() + '-' + rawDate.getFullYear();
console.log(parsedDate); // output could be: 19-08-2015 for example
Other possible ways of doing this could be found in here.

Convert date format 27.05.2015 01:46:32.UTC to Locale time

Can anyone help me to convert date format 27.05.2015 01:46:32.UTC to Locale time.
I tried the following code
var date = new Date('27.05.2015 01:46:32.UTC ');
date.toString();
This is also not working.
Even momentjs is also gives error to convert this format of date.
As per statement even momentjs is also gives error to convert this format of date, I have taken liberty and used momentjs here.
You need to use string-format moment constructor to pass string and format in which input string is.
Use
var t = "27.05.2015 01:46:32.UTC";
//pass dateTime string and its format, it will return
//moment object
var cdt = moment.utc(t, 'DD.MM.YYYY HH:mm:ss');
//Get date object
var date = cdt.toDate();
//Use date variable as per requiremnt
alert(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
The format you are using for Date is DD.MM.YYYY. So you have to change this to MM.DD.YYYY for a valid java script object.
var date = '27.05.2015 01:46:32.UTC';
var date1 = date.split(' ');
console.log(date1)
var date2 = date1[0].split('.');
console.log(date2)
var date3 = date2[1]+ '.' +date2[0] +'.'+ date2[2];
console.log(date3)
var final_date = date3 + ' ' + date1[1];
console.log(final_date);
final_date.toString();

How to format this JSON data to date(mm/dd/yyyy)

Sample JSON:
[{"DispatchDt":"2014-05-28T01:34:00","RcvdDt":"1988-12-26T00:00:00"}]
I have this set of dates and I want to convert it to the date format (mm/dd/yyyy). How do you do this is JavaScript?
Unfortunately parsing and formatting dates is a weak side of javascript.
So usually for that we use the 3rd party libraries like moment.js
An example of how you would do that with moment.js:
var date = '2014-05-28T01:34:00';
var parsedDate = moment(date);
var formattedDate = parsedDate.format('MM/DD/YYYY');
Demo: http://jsfiddle.net/8gzFW/
You may format the json string into new Date() then use js methods to get month,day,year or what exactly you need.
//format string to datetime
var DispatchDt = new Date(this.DispatchDt);
// then use this methods to fetch date,month, year needed
//getDate() // Returns the date
//getMonth() // Returns the month
//getFullYear() // Returns the year
var DispatchDt_date = DispatchDt.getDate();
var DispatchDt_month = DispatchDt.getMonth() + 1; //Months are zero based
var DispatchDt_year = DispatchDt.getFullYear();
Sample on jsFiddle
This would work
var a = "[{"DispatchDt":"2014-05-28T01:34:00","RcvdDt":"1988-12-26T00:00:00"}]";
var b = JSON.parse(c);
for (i in b[0]) {
b[0][i] = b[0][i].slice(0, 9)
}
console.log(b); // b now looks like this [ { DispatchDt: "2014-05-28", RcvDt: "1988-12-26" } ]
Please note the dates are stringified.

Categories

Resources