How to set Date formate in javascript? - 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

Related

How to convert the DD-MM-YYYY HH:mm format to epoch time using moment or js?

I have a date time picker and it omits date in format of a DD-MM-YYYY HH:mm a format. I want to convert it into epoch time to store it in the database backend.
I have tried using the following methods so far, but all of them return NaN as output:
var loadingPlannedUnTime = this.state.loadingPlannedUnTime;
console.log("normal conversion"+loadingPlannedUnTime);
//returns 9-08-2020 15:08:00
//Moment method
var loadingPlannedUnTime = moment(this.state.loadingPlannedUnTime).unix();
console.log("moemnt"+loadingPlannedUnTime)
//Returns NaN
//JS getTime() method
var pickupDateTime = new Date(this.state.loadingPickupTime);
var loadingPickupTime = pickupDateTime.getTime();
console.log("new date and gettime"+loadingPickupTime)
//Returns NaN
What is the correct method to convert it to epoch time?
https://momentjs.com/docs/
If you know the format of an input string, you can use that to parse a moment.
moment("12-25-1995", "MM-DD-YYYY");
const x = moment('24-12-2019 09:15', "DD-MM-YYYY hh:mm");
console.log(x.format())
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
const matches = loadingPlannedUnTime.match(/(\d{1,2})-(\d{1,2})-(\d{2,4}) (\d{1,2}):(\d{1,2}):(\d{1,2})/);
if (!!matches) {
// new Date(year, month, day, hours, minutes, seconds, milliseconds)
const epoch = new Date(matches[3], matches[2] - 1, matches[1], matches[4], matches[5], matches[6]).getTime();
console.log(epoch);
}

convert string to UTC time using jquery

I am trying to convert string to time, the string i have are in this format, '8:3' and '16:45'.
I want to convert UTC time in jQuery.
You can write your function to create UTC date with the time string.
function toUTC(str) {
let [h, m] = str.split(':');
let date = new Date();
date.setHours(h, m, 0)
return date.toUTCString();
}
console.log(toUTC('8:3'))
console.log(toUTC('16:45'))
You don't need jQuery for such operations. Just the simple Date object will do the trick. Say you want to convert time from a specific date.
let date = new Date('2020-04-01'); // leave the Date parameter blank if today
date.setHours(16); // must be 24 hours format
date.setMinutes(45);
let theUTCFormat = date.getUTCDate();
Cheers,

moment js displays milliseconds after AM/PM

Currently working on a project that deals with different languages and ran into a weird behavior when displaying milliseconds in the PM/AM format.
If I want to display a date/time pair with millisecond precision using the AM/PM format I get something like this:
12/14/2017 3:45:45 PM.199
Whereas the same date/time in a 24h format is shown like this:
12/14/2017 15:45:45.199
Is there a way to display the AM/PM format like this:
12/14/2017 3:45:45.199 PM
The objective is to dislpay the date/time according to the users localization options, that is why I'm using the "L LTS" formatting.
Here is a fiddle to illustrate the problem
var divEn = $('#divEn');
var divFi = $('#divFi');
var en = "en";
var pt = "pt";
var dateEn = moment().locale(en);
var dateFi = moment().locale(pt);
format = "L LTS.SSS";
divEn.text(dateEn.format(format));
divFi.text(dateFi.format(format));
If you always want to display result like 12/14/2017 3:45:45.199 PM use MM/DD/YYYY h:mm:ss.SSS A as format token instead of L LTS.SSS.
MM stands for number of month, DD is day of the month, YYYY is the year, h is the hour (0..12), mm stands for minutes, ss stands for seconds, SSS stands for fractional second and A stands for AM/PM.
L and LTS are localized tokens (output varies based on locale).
EDIT:
You can use localeData() and longDateFormat() go get localized format token, then you can check if the LTS token contains the AM/PM token (A or a) and change the format dinamically.
Here a live sample:
function getCustomLocalizedFormat(locale){
var localeData = moment.localeData(locale);
var lts = localeData.longDateFormat('LTS');
var format = "L LTS.SSS";
if( lts.match(/a/i) ){
format = "L h:mm:ss.SSS A";
}
return format;
}
var divEn = $('#divEn');
var divFi = $('#divFi');
var divRes = $('#divRes');
var en = "en";
var pt = "pt";
var dateEn = moment().locale(en);
var dateFi = moment().locale(pt);
var enLocaleData = moment.localeData('en');
var formatEn = getCustomLocalizedFormat('en');
var formatPt = getCustomLocalizedFormat('pt');
divEn.text(dateEn.format(formatEn));
divFi.text(dateFi.format(formatPt));
divRes.text(moment().format('MM/DD/YYYY h:mm:ss.SSS A'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment-with-locales.min.js"></script>
<p>English</p>
<div id="divEn"></div>
<p>Portuguese</p>
<div id="divFi"></div>
<p>General</p>
<div id="divRes"></div>
Please note that:
This approach is not test for each locale supported by momentjs. Locales like pa-in(Punjabi), ml (Malayalam), ne (Nepalese), hi (Hindi), gom-latn (Konkani Latin script) and others have the LTS token that starts with A. It's not clear to me what is the your expected output for these locales.
You can customize Long Date Formats using updateLocale function and the longDateFormat key.
Probably the best approach is to define a subset of supported locales and define custom format for each locale (and/or just customized moment's LTS).

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

Split Datetime to the clock

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"

Categories

Resources