Split Datetime to the clock - javascript

1. Question: How can I convert this.. 1970-01-01T08:00:00.000+01:00
to this: 08:00:00 or 08:00
i had use the following function:
var php = '{{ARBEITSZEITBIS}}';
var i = php.slice(0,20).split('-');
var ab = i[2];
but than come this output: 01T08:00:00.
I need a good function, not this function, because the datetime is in every document different
var d1 = Date.parse('2010-10-18, 10:06 AM');
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT'));
2. Question: I do an WKHTMLTOPDF Export with Placeholder, I have checkboxes, when I export this with the Placeholder {{incativ}} come out: true but i need: not given, how can Ireplace this via Jquery?

You can extract the time part without timezone info from an ISO 8601 timestamp with a couple split():
var timestamp = "1970-01-01T08:00:00.000+01:00";
var time = timestamp.split("T")[1].split(".")[0];
// time : "08:00:00"

Related

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>

Google Form on Submit get values and format the time

I am using Google Apps Script with a Google form. When the user submits the Google Form I get a value from a question. I then take that value and make it a date object, from what I saw on this post about daylight savings I use that to determine the timezone. I run the date object through Utilities.formatDate and want to get the correctly formatted date.
example: 9:00 AM
But instead I am getting a completely different time than expected.
My question is: Can someone help me understand why the below code is outputting a time that is 3 hours different?
function onSubmit(e) {
var values = e.values;
Logger.log(values);
try {
var start1 = new Date(values[3]);
var startN = new Date(start1).toString().substr(25,6)+"00";
var startT = Utilities.formatDate(start1, startN, "h:mm a");
Logger.log(startT);
} catch(error) {
Logger.log(error);
}
}
The assumption that Utilities formatDate does not support GMT... parameter is not true.
The post you mentioned in reference is used to get calendar events and is a useful way to get the right value when you get events from another daylight saving period (getting the TZ info from the calendar event itself), for example events for next month will be in "summer time" while we are still in "winter time"...
Your issue might come from different sources depending on time zone settings of your script vs timezone of the source. Could you describe the exact configuration in which you use this script ?
In the mean time, here is a small code that demonstrates how the code is working + the logger results :
function testOnSubmit() {
var eventInfo = {};
var values = {};
values['3'] = new Date();
eventInfo['values'] = values;
Logger.log('eventInfo = '+JSON.stringify(eventInfo)+'\n\n');
onSubmit(eventInfo);
}
function onSubmit(e) {
var values = e.values;
try {
var start1 = new Date(values[3]);
Logger.log('onSubmit log results : \n');
Logger.log('start1 = '+start1)
var startN = new Date(start1).toString().substr(25,6)+"00";
Logger.log('startN = '+startN);
var startT = Utilities.formatDate(start1, startN, "h:mm a");
Logger.log('result in timeZone = '+startT);
} catch(error) {
Logger.log(error);
}
}
EDIT : additionally, about the 30 and 45' offset, this can easily be solved by changing the substring length like this :
var startN = new Date(start1).toString().substr(25,8);
the result is the same, I had to use the other version a couple of years ago because Google changed the Utilities.formatDate method at some moment (issue 2204) but this has been fixed.
EDIT 2 : on the same subject, both methods actually return the same result, the GMT string has only the advantage that you don't have to know the exact timezone name... there is also the Session.getScriptTimeZone() method. Below is a demo script that shows the resulst for 2 dates in January and July along with the log results :
function testOnSubmit() {
var eventInfo = {};
var values = {};
values['3'] = new Date(2014,0,1,8,0,0,0);
eventInfo['values'] = values;
Logger.log('eventInfo = '+JSON.stringify(eventInfo)+'\n\n');
onSubmit(eventInfo);
values['3'] = new Date(2014,6,1,8,0,0,0);
eventInfo['values'] = values;
Logger.log('eventInfo = '+JSON.stringify(eventInfo)+'\n');
onSubmit(eventInfo);
}
function onSubmit(e) {
var values = e.values;
var start1 = new Date(values[3]);
Logger.log('onSubmit log results : ');
Logger.log('start1 = '+start1)
var startN = new Date(start1).toString().substr(25,8);
Logger.log('startN = '+startN);
Logger.log('result in timeZone using GMT string = '+Utilities.formatDate(start1, startN, "MMM,d h:mm a"));
Logger.log('result in timeZone using Joda.org string = '+Utilities.formatDate(start1, 'Europe/Brussels', "MMM,d h:mm a"));
Logger.log('result in timeZone using Session.getScriptTimeZone() = '+Utilities.formatDate(start1, Session.getScriptTimeZone(), "MMM,d h:mm a")+'\n');
}
Note also that the Logger has its own way to show the date object value ! it uses ISO 8601 time format which is UTC value.
Try this instead:
var timeZone = Session.getScriptTimeZone();
var startT = Utilities.formatDate(start1, timeZone, "h:mm a");
The Utilities.formatDate function expects a time zone that is a valid IANA time zone (such as America/Los_Angeles), not a GMT offset like GMT+0700.
I am making the assumption that Session.getScriptTimeZone() returns the appropriate zone. If not, then you might need to hard-code a specific zone, or use a different function to determine it.
Additionally, the +"00" in the script you had was assuming that all time zones use whole-hour offsets. In reality, there are several that have 30-minute or 45-minute offsets.

How to get time difference between two date time in javascript?

I want time duration between two date time. I have the start date, start time, end date and end time. Now I have to find the difference between them.
Actually I have tried with this following code, but I got the alert like 'invalidate date'.
function myfunction()
{
var start_dt = '2013-10-29 10:10:00';
var end_dt = '2013-10-30 10:10:00';
var new_st_dt=new Date(start_dt);
var new_end_dt=new Date(end_dt);
alert('new_st_dt:'+new_st_dt);
alert('new_end_dt:'+new_end_dt);
var duration=new_end_dt - new_st_dt;
alert('duration:'+duration);
}
the alert msg like as follows:
new_st_dt:invalid date
new_end_dt: invalid date
duration:NaN
when I run in android simulator I got these alert messages.
Please help me how to get it? How to implement this?
You're passing an invalid ISO date string to that Date() constructor. It needs a form like
YYYY-MM-DDThh:mm:ss
for instance
2013-10-29T10:10:00
So you basically forgot the T to separate date and time. But even if the browser reads in the ISO string now, you would not have an unix timestamp to calculate with. You either can call
Date.parse( '2013-10-29T10:10:00' ); // returns a timestamp
or you need to explicitly parse the Date object, like
var duration=(+new_end_dt) - (+new_st_dt);
Further read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Try formatting you timestamps as isoformat so javascript recognizes them. (you put a "T" between the date and time). An example: '2013-10-29T10:10:00'
function dateDiff(){
var start_dt = '2013-10-29 10:10:00';
var end_dt = '2013-10-30 10:10:00';
var d1= start_dt ;
d1.split("-");
var d2= end_dt ;
d2.split("-");
var t1 = new Date(d2[0],d2[1],d2[2]);
var t2 = new Date(d1[0],d1[1],d1[2]);
var dif = t1.getTime() - t2.getTime();
var Seconds_from_T1_to_T2 = dif / 1000;
return Math.abs(Seconds_from_T1_to_T2);
}

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

How to set Date formate in javascript?

I want to convert datetime format for my radtime picker. I am getting 2012-8-2-13-00-00
as my output when I pick date from my radtime picker. When I try to convert in to date it is saying invalid date.
The JavaScript:
function SourqtyValidation()
{
var specifiedtime = document.getElementById('<%=txtSpecifiedArrvialTime.ClientID %>').value;
alert(specifiedtime);
var a = new Date(specifiedtime);
var actuvalarrivaltime = document.getElementById('<%=txtActualArrivalTime.ClientID %>').value;
alert(actuvalarrivaltime);
var b = new Date(actuvalarrivaltime);
b.format("dd/MM/yyyy hh:mm:ss");
alert(b);
var difference =Math.round((a - b) / 1000);
alert(difference);
}
The aspx:
//txtSpecifiedArrvialTime = predifened as 2012/08/02 09:35:55;
<telerik:RadTimePicker ID="txtActualArrivalTime" runat="server" EmptyMessage="Actual Arrival Time"> </telerik:RadTimePicker>
So how can I get difference between two times in minutes?
to get the time difference,date should be in same format,
"2012-8-2-13-00-00" is not the correct format, convert it into format of "2012/08/02 13:00:00"
than you can get the differencein in second by dividing it to 1000.
you can use this to convert your string to datetime
var dt = '2012-8-2-13-00-00'.split(/\-|\s/);
dat = new Date(dt.slice(0,3).reverse().join('/')+' '+dt[3]+':'+dt[4]+':'+dt[5]);
Try using Datejs it has very handy methods:
Eg.:
Date.parse('Thu, 1 July 2004 22:30:00');
Date.parseExact("10/15/2004", "M/d/yyyy"); // The Date of 15-Oct-2004
Very Good library for handling Data Time in javascript.
Datejs is an open-source JavaScript Date Library.
Datejs

Categories

Resources