converting a date to european format - javascript

I am writing a script for automating XML data in Indesign. I currently have in the XML the date in American Format (MM/D/YY) but once I run the script in Indesign my goal is to have it be in European format (DD/M/YY). What can I add to my script in order to write a function that will convert any date formats to european formats? I hope this makes sense. I need help!

i guess simply using
function convertDate(dateString) {
var date = new Date(dateString);
return date.getDate()+"/"+(date.getMonth() + 1)+"/"+date.getFullYear();
}
just note european format does not use slashes but dots as far as i know so it should look like this
dd.mm.yyyy
not
dd/mm/yyyy
becouse it can be mismatched with us format

How to format a JavaScript date?
Return dd-mm-yyyy from Date() object
var d = new Date().toLocaleDateString("uk-Uk");
Output: 03.09.2021
var d = new Date().toLocaleDateString("en-GB");
Output: 03/09/2021
var d = new Date().toLocaleDateString("en-UK").replace(/\//g, '-');
Output: 03-09-2021

var myDate = new Date('myDateString'); //you can also do milliseconds instead of the date string
var myEuroDate = myDate.getDate() + '/' + myDate.getMonth + '/' + myDate.getFullYear();
Useful as well...
http://arshaw.com/xdate/

Related

How to parse a date like '2009-09-13' in D3.JS or moment

I have several dates as strings that are in the format: YYYY-MM-DD. So for example:
var origDate = '2009-09-13'
I want to parse them so that they're just written out as month (abbreviated or full month), day, year like: Sept. 9, 2009
..Is there a way to do this in D3.JS or Moment.JS?
Right now I'm using this D3 formula to parse the dates:
var format = d3.time.format("%Y-%m-%d").parse;
But when I do: format(origDate),
I get really long dates like:
Is there a way to do this in D3.JS?
Yes. Your line
var format = d3.time.format("%Y-%m-%d").parse;
is creating a date object. You need to apply formatting to the object to get the desired output string:
// working date:
var string = '2009-05-01';
// set up a format
var format = d3.time.format("%Y-%m-%d");
// convert the date string into a date object:
var date = format.parse(string);
// output the date object as a string based on the format:
console.log(format(date));
// use the desired format
var format1 = d3.time.format("%B %d, %Y");
// output a date as a string in the desired format
console.log(format1(date));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Using moment do it like this:
var origDate = '2009-09-13'
mDate = moment(origDate);
console.log(mDate.format("DD/MM/YYYY"));
https://jsfiddle.net/0p2c6zjo/
Vanilla JS
Use the new Date constructor on your string (it is in a supported format), and then use a format function to map each month to its abbreviation and include other information.
var abbr = 'Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.'.split(' ')
function format (date) {
return abbr[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear()
}
var origDate = '2009-09-13'
console.log(
format(new Date(origDate))
)
Moment.js
Moment.js objects have a format function that takes a format string. For the full list of parameters you can use, see the documentation on displaying.
var origDate = '2009-09-13'
console.log(
moment.utc(origDate).format('MMM. D, YYYY')
)
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>

Parse date in javascript issues

I get such date in javascript
var val = "1960-05-15T20:00:00"
But if I do
var date = new Date(val);
The data I get is one day later:
1960-05-16 // I use this to obtain it: Ext.Date.format(new Date(val), 'm/d/Y')
Can you help me how to parse this date? and get correct date with 1960-05-15?
Your date format is ISO 8601 represented as the local time with an offset to UTC appended.
The Ext.Date singleton support this format with the c flag.
var parsedDate = Ext.Date.parse('1960-05-15T20:00:00', 'c');
var dateStr = Ext.Date.format(parsedDate, 'Y-m-d');
// "1960-05-15"
Have a look at the Sencha ExtJs 6.2.1 documentation Ext.Date for further informations.
You can use native JS to accomplish the output of the Date object in to this format yyyy-mm-dd
Like so:
var val = '1960-05-15T20:00:00';
var d = new Date(val);
var date = d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2);
console.log(date);
When you do
var a = new Date(someDate)
variable a contains date according to your local timezone.
If you want date in same format as you entered , use toISOString method
var a = (new Date(someDate)).toISOString()
My recommendation would be, you can only assume the timezone from where the date is coming from. If you know exactly where that date is coming from, a.k.a London, New York, Sidney, etc... then you can use momentjs to set the UTC offset
var val = "1960-05-15T20:00:00"
// these are equivalent
moment(val).utcOffset("+08:00");
moment(val).utcOffset(8);
moment(val).utcOffset(480);
So the OP has said they're in
Tbilisi, Georgia GMT + 4.00
so
moment(val).utcOffset("+04:00");

How To Convert String ' dd/mm/yy hh:MM:ss ' to Date in javascript?

I have Done this
var d='dd/mm/yy hh:MM:ss';
var d1=d.split(" ");
var date=d1[0].split("/");
var time=d1[1].split(":");
var dd=date[0];
var mm=date[1]-1;
var yy=date[2];
var hh=time[0];
var min=time[1];
var ss=time[2];
var fromdt= new Date("20"+yy,mm-1,dd,hh,min,ss);
Is there Any way to do it using JQuery OR JavaScript?
If you are looking for alternatives in jquery or Javascript , then you can go with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.
example:
var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');
This will work regardless of timezone for the format dd/mm/yyy hh:mm:ss only. It also does not rely on third party packages:
let dtStr = "12/03/2010 09:55:35"
console.log(strToDate(dtStr)); // Fri Mar 12 2010 09:55:35
function strToDate(dtStr) {
if (!dtStr) return null
let dateParts = dtStr.split("/");
let timeParts = dateParts[2].split(" ")[1].split(":");
dateParts[2] = dateParts[2].split(" ")[0];
// month is 0-based, that's why we need dataParts[1] - 1
return dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0], timeParts[0], timeParts[1], timeParts[2]);
}
How about Date.parse()?
new Date( Date.parse("05/12/05 11:11:11") );
// Thu May 12 2005 11:11:11 GMT+0200 (CEST)
The output produced is in local timezone and will differ in browsers in different timezones.
We can convert any local date format to datetime datatype using moment js.
Syntax:
moment('<local date value>','<local date format>').format('<expected convert format>')
Example:
moment('26/05/1986 00:00', 'DD/MM/YYYY HH:mm').format("MM/DD/YYYY HH:mm");
then the output will be 05/26/1986 00:00
Cheers
Date#parse should be able to parse that if you split on a string. Id also recommend looking into the npm package moment for date manipulation
From your code it seems that you are trying to convert string into date and also you are trying to fetch previous month. If yes then you can reconstruct your code as below:
Date.prototype.SubtractMonth = function(numberOfMonths) {
var d = this;
d.setMonth(d.getMonth() - numberOfMonths);
return d;
}
$(document).ready(function() {
var dateString='2015-06-17T18:30:12';
var d = new Date(dateString);
alert(d.SubtractMonth(1));
});

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();

Converting a date in an invalid javascript format to an ics standard datetime field

The date and time are actually separate in the data I have however I thought combining them would be the ideal solution?
The unaltered data looks like the following:
2014/09/04 and 02:30PM
var aDate = new Date('2014/09/04 02:30PM');
//Invalid date....
console.log(aDate.toString());
Needs to convert to the date that .ics files use which looks like:
20140904T023000 --> This is what the above date would turn into.
How do I do this?
JSBin you could test in...
Change
var aDate = new Date('2014/09/04 02:30PM');
to
var aDate = new Date('2014/09/04 02:30 pm');
and it should work. You just need to put in a SPACE before PM. This will not give you invalid date error. Then you can use that date to get ICS format.
Here is the logic to get ICS format. I know this is not as efficient, but tried to keep it simple.
var pre =
aDate.getFullYear().toString() +
((aDate.getMonth() + 1)<10? "0" + (aDate.getMonth() + 1).toString():(aDate.getMonth() + 1).toString()) +
((aDate.getDate() + 1)<10? "0" + aDate.getDate().toString():aDate.getDate().toString());
var post = (aDate.getHours()%12).toString() + aDate.getMinutes().toString() + "00";
console.log(pre + "T" + post);
You should be able to parse the date fine if you use 24 hour notation for time rather than denoting AM/PM.
From Chrome console:
(new Date('2014/09/04 02:30PM'));
Invalid Date
(new Date('2014/09/04 14:30'));
Thu Sep 04 2014 14:30:00 GMT+0100 (BST)
With this object, you should be able to construct the ICS format compliant date you require using the various getters on the Date prototype.
There are no default api`s to convert to .ics file format, you can however do it manually.
function toITCFormat(date, time)
{
var timeCont = [],
dateCont = [];
if(time.toLowerCase().indexOf('pm')!=-1)
{
timeCont = time.toLowerCase().replace('pm',00).split(':'); //assuming from your question seconds is never mentioned but only hh:mm i.e. hours and minutes
timeCont[0] = (parseInt(timeCont[0])+12)%24;
}
else
{
timeCont = time.toLowerCase().replace('am',00).split(':');
}
dateCont = date.split('/');
return dateCont.join('')+'T'+timeCont.join('');
}
var x = toITCFormat('2014/09/04','02:30PM');
console.log(x);// this will output ur .ics format
JSFiddle Example

Categories

Resources