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.
Related
I'm using javascript and trying to convert a string to a different format. I did some research on this format but no one has asked about it yet so I thought I may ask. So I have seen that others want to convert a date string that has a shorter length. But this format is different. The format I have now by doing this:
const now = new Date();
const currentDate = now.toISOString();
I get this number:
2021-12-05T07:52:47.485Z
However, I want to make it the format like this:
2021-12-05T00:00:00.000+00:00
Is there any way to do so? I don't see others asking about this so not sure if possible
Use moment library:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Like this:
var now = new Date();
now.setHours(0,0,0,0);
var format = 'YYYY-MM-DD[T]HH:mm:ss.SSS[+00:00]';
console.log(moment(now).format(format));
Link: https://codepen.io/sdssz1365/pen/rNGORKR
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);
My value contains "08.07.1987", how to retrieve the date object for this string. new Date(val) gives correct date object values only for the string value that contains "/" format. can any one let me know hot to create date object for the values which contains "." or "-". in its format.
How about just adjusting the string to suit your needs?
var date1 = new Date("08.07.1987".replace('.','/'));
var date2 = new Date("08-07-1987".replace('-','/'));
You will need to be careful when asking Javascript to interpret a date in this format. As you can probably imagine, a date listed as "08.07.1987" doesn't really specify whether it's August 7th or July 8th.
In general, your best bet will be to specify a date format and parse accordingly.
you have to split the string into tokens for month date and year and then create it using JS Date API.
var date="08.07.1987";
var newDate = date.replace(/(\.|-)/g,"/"));
var dateObject = new Date(newDate);
Replace the delimiters?
var dateStr = "08.07.1987",
dateObj = new Date(dateStr.replace(/[-.]/g,"/"));
Of course you can encapsulate that in a function if need be...
try this new Date("08.07.1987".replace('.','/','g')); tested on firefox only
I have a date in this string format "02/28/2012" and I want to convert it to UTC.
I'm using the jquery datepicker to select thedate and populate an inputbox. any clues?
Thanks
var datestr = "07/08/2005";
var datearr = datestr.split("/")
var utc = Date.UTC(datearr[2],datearr[0],datearr[1]);
var utcdate = Date.UTC(2012,2,28);
The other answers are good, but they will give you the wrong result.
In Javascript, the month argument is zero-indexed, so make sure to subtract 1 from the standard month number,
var utcms = Date.UTC(2012,2-1,28);
Unfortunately jquery .datepicker.parseDate(str) injects a local timezone (it would be nice if the documentation said this), and Date(str) and Date.parse(str) appear unpredictable about their treatment of local vs UTC.
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('-'));