I get a batch report with an odd date format of dd.mm.yyy and I would like to automatically be able to convert them all to something google understands is a date, like mm/dd/yyyy. Any help would be awesome. I am a n00b with regex.
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var text = doc.editAsText();
// Change up the date format
text.replaceText("c?c.c?c.cccc", "/");
}
you could split the date based on the delimiter and then mash them back together how you want with something like this:
function myFunction() {
text = '12.03.012'
textArray = text.split('.')
text = textArray[0]+'/'+textArray[1]+'/2'+textArray[2]
Logger.log(text)
}
Logging Output shows:
12/03/2012
I would use momentjs for that. That way you can go directly to a javascript date object, and don't have to mess around with regExps. You can use their parsing string-format api to convert your format to standard.
var legitDate = moment(oddlyFormatedDate, "MM-DD-YYYY"); // use MM DD etc to describe your odd date format.
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 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.
This question already has answers here:
How to parse a date in format "YYYYmmdd" in JavaScript?
(6 answers)
Closed 6 years ago.
I am pulling a date of birth as a string from a form on a website im looking after. I need to be able to derive an age from that date string.
My thoughts are converting it into a proper date using a split (its delimited by "%2F") and calculating from that but my syntax isnt that good so im having real trouble.
The code im working with to pull the string I need is;
function() {
var inputField = document.getElementById("date-of-birth-input");
return inputField.value || "";
}
Any help would be appreciated.
If you have the user input the date in a format like 1/1/1990, you could do something like this:
var inputField = document.getElementById("date-of-birth-input"); // `1/1/1990`
var today = new Date().getFullYear();
var birthdate = new Date(inputField).getFullYear();
var age = today - birthdate;
The JavaScript Date constructor is pretty flexible in what it can turn into a date, so you might not need to go through splitting-string hassles. Check out the docs.
Date format varies in different countries. In US the date format is MM-DD-YYYY while in Asian countries the date format is DD-MM-YYYY and in China its 'YYYY-MM-DD'. The right way to go about dealing with dates is using Moment.js
If you have a simple application/website include moment in your <head> tag as follows
<script src="moment.js"></script>
(Hoping you have downloaded moment.js and kept in the same structure as index.html). Otherwise use this if you want to use it directly from the net without downloading.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/locale/af.js"></script>
Then use the following
function() {
/* First-make sure you format moment according to your giving locale.
I'm formating here according to the US date format */
var inputField = moment(document.getElementById("date-of-birth-input")).format("MM-DD-YYYY");
return moment().diff(inputField, 'years');
}
I hope this helps. Thanks.
I have the following HTML:
<strong id="ut-open-date">27/06/2014</strong>
and I want to read the text/innerhtml and convert its format to "YYYY-MM-DD" so I can insert it into MySQL table. I am using the moment.js library and my code is below:
var CreateDate = moment(jQuery('#ut-open-date').html()).format("DD/MM/YYYY");
CreateDate = moment(CreateDate).format("YYYY-MM-DD");
But the code changes 27/06/2014 to 2016-06-03 and I cannot work out why.
I also tried this code with the same result.
var CreateDate = moment(jQuery('#ut-open-date').html()).format("YYYY-MM-DD");
Any help is appreciated.
If we break down your code step by step you can see where it is going wrong:
var CreateDate = moment(jQuery('#ut-open-date').html())
This part uses the default constructor to try to parse the date, this is unreliable at best and has been deprecated. So moment is trying to guess what the date format is here.
.format("DD/MM/YYYY");
This is taking what ever was read in step 1 and trying to turn it into a string with the format of DD/MM/YYYY
CreateDate = moment(CreateDate)
Now you are parsing again without specifying the format so moment is doing it's best to guess
.format("YYYY-MM-DD");
Now you have told it to turn whatever it guessed the date to be into a string with the format YYYY-MM-DD
Do this instead:
var CreateDate = moment(jQuery('#ut-open-date').html(), 'DD/MM/YYYY').format('YYYY-MM-DD');
The moment(dateString) form is deprecated you should use the form moment(dateString, expectedFormat) instead.
See moment documentation here: http://momentjs.com/docs/#/parsing/string-format/
Thanks to athms for link
The problem is you need to tell moment.js what format for date string you want parse by specifying second parameter. See all the supported format. If your format is not listed (See the Open Issue for NON-ISO strings), you need to specify the date format parameter.
moment( jQuery('#ut-open-date').html(), "DD/MM/YYYY" )
DEMO
I solved it by using split as follows:
var CreateDate = jQuery('#ut-open-date').text();
var DateArray = CreateDate.split('/');
CreateDate = DateArray[2] + '-' + DateArray[1] + '-' + DateArray[0];
I want to create a Date object in Javascript using this string 04/21/2014 12:00p
When passed to the constructor (new Date('04/21/2014 12:00p')), it returns Invalid Date.
I've seen other posts which manipulate the string in order to fulfill the requirements of a valid dateString, however that is not what I want. I want Javascript to recognize my date format (m/dd/yy h:mmt). In Java, something like that is simple, I imagine that there would be a similar way in Javascript.
How can I get the Date object to recognize my format?
This is trivial only when using a library like moment.js:
var dt = moment("04/21/2014 12:00p","MM/DD/YYYY h:mma").toDate();
Otherwise, you would have considerable string manipulation to do. Also you would have to account for users in parts of the world that use m/d/y or other formatting instead of the y/m/d formatting of your input string.
If this string is being sent from some back-end process, you might consider changing the format to a standard interchange format like ISO-8601 instead. Ex. "2014-04-21T12:00:00"
To manipulate the string in order to fulfill the requirements, could be a way, but you need to take care of all browser issues.
A more quick and dirty way is use moment.js library. It helps on formatting matters too.
if (String.prototype.dateFromJava == null)
{
String.prototype.fromJava = function (sDateString)
{
var aDateOrTime = sDateString.splt(" ");
var aDateParts = aDateOrTime[0].split("/");
var aTimeParts = aDateOrTime[1].split(":");
var oDate = null;
/* just get the pieces and passing them in to new Date(), return oDate */
}
}