I have a date returned in this format YYYY-MM-DD, e.g. 2011-04-29. Using jQuery how can I make the date 29-04-2011?
You can use .reverse()
var pieces = '2011-07-27'.split('-');
pieces.reverse();
var reversed = pieces.join('-');
var parts = '2011-07-27'.split(/-/);
parts.reverse();
alert(parts.join('-'));
Related
I am using a library to calculate the age from date of birth. I am taking date of birth as an input which is in the format of dd/mm/yy but the library that calculated the age accepts it in the format of mm/dd/yy. One solution to this is to change the date selector format in the application but I dont want to do that since it gets confusing.
I searched the solution on stackoverflow but couldnt find the solution here- How to format a JavaScript date
How about a simple split and join:
var yourDate = "10/12/2021";
var arrayOfDate = yourDate.split("/");
console.log([arrayOfDate[1], arrayOfDate[0], arrayOfDate[2]].join('/'));
Just split it with / and then use destructure it and get the desired result as:
const result = `${mm}/${dd}/${yy}`;
var oldDate = "10/12/21";
var [dd, mm, yy] = oldDate.split("/");
const newDate = `${mm}/${dd}/${yy}`;
console.log(newDate);
I have getting a dateformat like '23.10.2017'. I need to format this in to
'10/23/2017'
I just tried
var crDate='23.10.2017';
var newDateF=new Date(crdate).toUTCString();
but it showing InvalidDate
can anyone help to change the format.
Thanks in advance
I don't think using Date() is the solution. You can do
var crDate = '23.10.2017';
var newDateF = crDate.split(".");
var temp = newDateF[0];
newDateF[0] = newDateF[1];
newDateF[1] = temp;
newDateF.join("/");
This splits the string into an array, swaps the first and second elements, and then joins back on a slash.
A regex replacement will do the trick without any Date functions.
var date = '23.10.2017';
var regex = /([0-9]{2})\.([0-9]{2})\.([0-9]{4})/;
console.log(date.replace(regex,'$2/$1/$3'));
Just use moment.js if you can :
// convert a date from/to specific format
moment("23.10.2017", "DD.MM.YYYY").format('MM/DD/YYYY')
// get the current date in a specific format
moment().format('MM/DD/YYYY')
Moment is a very usefull and powerfull date/time library for Javascript.
I have a javascript datetime picker which generated a time in this format: 2017-09-03T11:37:00.000Z
Now, I am trying to set default value of december 31, 2007 but I don't know how to generate it with the format the datepicker uses.
I have done jsfiddle:
var original = '2017-09-03T11:37:00.000Z';
var recreate = moment().endOf('year').format('YYYY-MM-HH:MM-SS:MS');
alert(recreate) // 2017-12-23:12-99:129
but the format isn't the same
This format is known as ISO date format. You can convert moment to this format by calling corresponding function:
var original = '2017-09-03T11:37:00.000Z';
var recreate = moment().endOf('year').toISOString();
alert(recreate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
See updated fiddle
toDate() to get it in correct format.
check this
var original = '2017-09-03T11:37:00.000Z';
var recreate = moment().endOf('year');
alert(moment(recreate).toDate());
I am using moment.js for the time format. SO right now I am getting UTC time(get time from server which is locate other timezone), SO I want to convert into ISO 8601. I am able to convert single value UTC to ISO. But I cant convert whole array into ISO Format.
var time= [11:30,11:50,12:50,22:30,23:10,2:20,4:30,8:50,9:10]
I want to each value convert into ISO format.
You can loop through you array, parse your input using moment(String, String) and then use toISOString() or format().
Here a live sample:
var time= ['11:30','11:50','12:50','22:30','23:10','2:20','4:30','8:50','9:10'];
var result = time.map(elem => moment.utc(elem, 'HH:mm').toISOString());
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Use map()
var time = ["11:30","11:50","12:50","22:30"]
.map(el => moment(el, 'hh:mm'));
What about iterating over the values and replace them?
for (i = 0; i < time.length; i++) {
time[i] = moment(time[i], 'hh:mm');
}
You can use map to convert each value:
let converted = time.map(t => t.toISOString());
t.toISOString() - it's just for example you can add any logic and return converted time.
How can i change the current date to this format(DD/MM/YYYY) using moment.js?
I have tried below code.
$scope.SearchDate = moment(new Date(), "DD/MM/YYYY");
But it's return 0037-11-24T18:30:00.000Z. Did't help to format current date.
You need to call format() function to get the formatted value
$scope.SearchDate = moment(new Date()).format("DD/MM/YYYY")
//or $scope.SearchDate = moment().format("DD/MM/YYYY")
The syntax you have used is used to parse a given string to date object by using the specified formate
You can use this
moment().format("DD/MM/YYYY");
However, this returns a date string in the specified format for today, not a moment date object. Doing the following will make it a moment date object in the format you want.
var someDateString = moment().format("DD/MM/YYYY");
var someDate = moment(someDateString, "DD/MM/YYYY");
This worked for me
var dateToFormat = "2018-05-16 12:57:13"; //TIMESTAMP
moment(dateToFormat).format("DD/MM/YYYY"); // you get "16/05/2018"
This actually worked for me:
moment(mydate).format('L');
for anyone who's using react-moment:
simply use format prop to your needed format:
const now = new Date()
<Moment format="DD/MM/YYYY">{now}</Moment>
A safe way to do this
moment.locale('en-US');
moment().format("L");
"06/23/2021"
moment.locale('fr');
moment().format("L");
"23/06/2021"