Property date to String for parsing - javascript

I'm trying to insert creation date into table view in specific format.
Now it's like DD/MM/YYYY HH:MM:ss and I want it like DD/MM/YYYY.
YAHOO.Bubbling.fire("registerRenderer", {
propertyName: "test:date",
renderer: function functionPrice(record, label){
var jsNode = record.jsNode,
properties = jsNode.properties;
var rawDate = properties['test:date'];
var date= rawDate().toString().substring(0, 11);
return '<div id="attachments">' + date + '</div>';
}
});
In this case, column contains [Object obj.
I also tried convert it to toISOString but it returns Invalid Date.
Column is set like d:date but output is d:datetime and I don't know why.
Thank you.

If your date format is fixed, this is a safe way to create a Date instance:
var value = "31/12/2017 00:00:00";
var dd = value.substring(0,2);
var mm = value.substring(3,5);
var yyyy = value.substring(6,10);
var d = new Date(yyyy, mm - 1, dd); // Sun Dec 31 2017 00:00:00 GMT+0800 (+08)

To change dates the dates displayed in the date picker control, but this file may not exist in your environment. See if the following file exists:
<alfresco home>\tomcat\shared\classes\alfresco\web-extension\site-webscripts\org\alfresco\components\form\form.get_en.properties
If it doesn’t exist, copy it from here (create the form folder if necessary):
<alfresco home>\tomcat\webapps\share\WEB-INF\classes\alfresco\site-webscripts\org\alfresco\components\form\form.get_en.properties
Open the form.get_en.properties file for editing. Search for “form-control.date-picker” to find the proper properties to change (we found four values on a recent installation).
Restart Alfresco to make the changes take effect.

Related

How to format a Date in a Specific Format in React.js?

I want to show the full date formatted from this 2020-11-09T17:50:00.000Z
to this 22/1/2020 14:20:22 format. I know how get the desired format via moment.js, but want to achieve this with JavaScript Date.
Here is what I have now, but this is not what I want.
let d = new Date("2020-11-09T17:50:00.000Z".toLocaleString("en-US"))
console.log(d);
Any help will be appreciated
You can always do it manually, the Date API only has a limited set of functions like .toLocaleDateString() which will give you "11/9/2020" and .toGMTString() will return "Mon, 09 Nov 2020 17:50:00 GMT".
Using your Date APIs, you can build the string yourself using what you have.
var timeString = d.toGMTString().split(" ")[4]; //This will return your 17:50:00
//For the date string part of it
var dateNumber = d.getDate();
var monthNumber = d.getMonth() + 1;
var yearNumber = d.getFullYear();
var dateString = `${dateNumber}/${monthNumber}/${yearNumber}`;
var finalDateString = [dateString, timeString].join(" ");
toLocaleString() can produce many formats, and you can choose the locale to get the format (or close to it) that you want.
The locale "en-GB" gives you almost what you want; you just need to remove the comma that it puts in...
let d = new Date(2020, 0, 22, 14, 20, 22);
let output = d.toLocaleString("en-GB")
.replace(',' ,'');
console.log(output);
You can actually control the output further by using the options parameter.
But also see the Intl object for its DateTimeFormat constructor.

What does my date time get converted into from ASP.Net MVC to Javascript

In my html file I'm passing an array of objects with a date time to javascript like this
<script>
var TimerEvents = #Html.Raw(Json.Encode(Model.PendingTimerEvents));
</script>
my 'TimerEvents' has an EventTime property that when I read in Javascript looks like this
"/Date(1521617700000)/"
and I want to get this value, whatever it is, into a Javascript Date() object.
like this
var countDownDate = new Date(date here).getTime();
What is that format, and how would I do this?
the value you get is a milisecond representation of the date, I think the count starts at 1/1/1970.
Anyway here is a solution for formatting it:
Converting milliseconds to a date (jQuery/JavaScript)
Assuming that Model.PendingTimerEvents is a DateTime, you could just do:
var TimerDate = new Date(#Model.PendingTimerEvents.Year, #Model.PendingTimerEvents.Month, #Model.PendingTimerEvents.Day, #Model.PendingTimerEvents.Hour, #Model.PendingTimerEvents.Minute, #Model.PendingTimerEvents.Second);
You can extract the date with regex if you need to:
<script>
var TimerEvents = #Html.Raw(Json.Encode(Model.PendingTimerEvents)); //"/Date(1521617700000)/"
var dateString = TimerEvents.match(/\d+/)[0] // "1521617700000"
var date = new Date(+dateString); //convert to number
console.log(date); // Wed Mar 21 2018 08:35:00 GMT+0100 (Mitteleuropäische Zeit)
</script>

Date and time based json element using javascript

I have a json response like this :
{
"NO_INSPECTION": "55",
"NO_SURAT": "00055",
"DATE_OF_DESCRIPTION": "2015-12-21 03:08:24"
}
How can I convert the data in "DATE_OF_DESCRIPTION" Into date and time. Date should be dd-mm-yyy format and time should be in HH:mm format. (A sample value of DATE_OF_DESCRIPTION is 2015-12-21 03:08:24)
I have tried new Date(response.DATE_OF_DESCRIPTION); but no success. How can I achieve this?
Without the use of other libraries and assuming the output will always be zero-padded and the same length, I would do this:
var response = {
DATE_OF_DESCRIPTION: "2015-12-21 03:08:24"
}
var raw = response.DATE_OF_DESCRIPTION;
var datePart = raw.split(' ')[0];
var timePart = raw.split(' ')[1];
var year = datePart.substring(0, 4);
var month = datePart.substring(5, 7);
var day = datePart.substring(8, 10);
var hours = timePart.substring(0, 2);
var minutes = timePart.substring(3, 5);
// NOTE: Month is 0 indexed
var date = new Date(year, month - 1, day);
var dateTime = new Date(year, month - 1, day, hours, minutes);
console.log(date);
console.log(dateTime);
This gives the output
Mon Dec 21 2015 00:00:00 GMT+1000 (E. Australia Standard Time)
Mon Dec 21 2015 03:08:00 GMT+1000 (E. Australia Standard Time)
(I'm from Australia, so your timezone will vary)
JavaScript has a fixed date format and you can change it, thus the Date object won't help you this time. As I see it, you want to split that date, so it's pretty easy if you provide it in this format "dd-mm-yyy HH:mm":
response.DATE_OF_DESCRIPTION = response.DATE_OF_DESCRIPTION.split(" "); // date and time are separated by an space
var date = response.DATE_OF_DESCRIPTION[0];
var time = response.DATE_OF_DESCRIPTION[1];
BTW, if you want to parse a date in a specified format, why don't you use any library for that? Many of them are almost as reliable and fast as native methods. Give them a try ;)
You could also format the date, so it fits the JS specs but, why reinvent the wheel? Libraries will do this for you and you'll get optimal cross-browser results!
I've googled "javascript date parsing library" and this is what I've found:
http://momentjs.com/ <--- I think that's what you're looking for!

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

json datetime to javascript datetime gets me wrong date

I am trying to convert datetime from the server in the form of JSON to something I can use in HighChart but I keep getting the wrong day.
var json = {"lastMinute":"2013-05-06 15:46:00"}; // GMT
var lastMinute = json.lastMinute;
var a = lastMinute.split(' ');
var d = a[0].split('-');
var t = a[1].split(':');
var date = new Date(d[0],d[1],d[2],t[0],t[1],t[2]);
// NEED TO CONVERT TO LOCALTIME
this outputs: Thu Jun 06 2013 15:46:00 GMT-0700 (PDT)
can someone put my out of my misery and tell me what stupidly simple thing I'm doing wrong?
in js, months start with 0 instead of 1...
var json = {"lastMinute":"2013-05-06 15:46:00"}; // GMT
var lastMinute = json.lastMinute;
var a = lastMinute.split(' ');
var d = a[0].split('-');
var t = a[1].split(':');
var date = new Date(d[0],d[1]-1,d[2],t[0],t[1],t[2]);
You can use Datejs (https://code.google.com/p/datejs/wiki/APIDocumentation) library. It is very usefull and you have the metod parseExact:
Date.parseExact("10/15/2004", "M/d/yyyy");
For ultimate in parsing and formatting dates in JavaScript, use the moment.js library.
You can be explicit about UTC, and this works just fine in downlevel browsers.
var m = moment.utc('2013-05-06 15:46:00');
These values look like they are in ISO8601 format (without the T). So you should be ok with just the above code. But if you want to be even more precise, you could do:
var m = moment.utc('2013-05-06 15:46:00', 'YYYY-MM-DD HH:mm:ss');
I would recommend keeping it as a moment for as long as possible. Their formatting is superb. But if you need it back as a javascript date, just do:
var date = m.toDate();

Categories

Resources