Timestamp Splitting incorrect - Javascript - javascript

I've a Timestamp loaded from MSSQL into a Webclient.
The Timestamp is: Thu Jan 01 18:00:00 CET 1970,
but i need only the: 18:00 or 18:00:00 (first one is the better one..)
<script>
var timestamp = "{{SYSTIMESTAMP}}";
var time1 = timestamp.split("T")[1].split(".")[0];
</script>
The HTMLcode for the function is:
<span class="systime"><script type="text/javascript">document.write(time1)</script></span>
The output is:
hu Jan 01 18:00:00 CE
--> I need 18:00 or 18:00:00

The Timestamp is: Thu Jan 01 18:00:00 CET 1970
Assuming you have that as a string, client side:
var str = "Thu Jan 01 18:00:00 CET 1970";
The simplest way to get "18:00" from it is a simple regular expression:
var m = /(\d{2}:\d{2}):\d{2}/.exec(str);
var result = m && m[1];
You said just "18:00" was better, but if you wanted "18:00:00", just move the closing ) to the end, just before the / that ends the regular expression.
Live example:
var str = "Thu Jan 01 18:00:00 CET 1970";
var m = /(\d{2}:\d{2}):\d{2}/.exec(str);
var result = m && m[1];
snippet.log("Result: '" + result + "'");
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Related

Date not parsing correct in Javascript

I'm using timestamp that is inserted into my PostreSQL database & trying to parse it to become user friendly, however I'm getting the wrong year?
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
let timestamp = toTimestamp('Sun Jan 19 2020 21:19:40 GMT+0000 (Coordinated Universal Time)');
var d = new Date();
d.setTime(timestamp);
console.log(d.toGMTString()); //Mon, 19 Jan 1970 06:44:28 GMT
I'm expecting a result of Sun, 19 Jan 2020 21:19:40 GMT
Don't divide datum by 1000
see here
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum;
}
let timestamp = toTimestamp('Sun Jan 19 2020 21:19:40 GMT+0000 (Coordinated Universal Time)');
var d = new Date();
d.setTime(timestamp);
console.log(d.toGMTString()); // Sun, 19 Jan 2020 21:19:40 GMT
It's just a unit of measurement error. Date expects epoch in milliseconds but you are dividing the datum variable by 1000, turning it into seconds. This is resulting in the discrepancy and can be fixed by removing the divide by 1000 step.
toTimestamp then becomes:
function toTimestamp(strDate){
return Date.parse(strDate);
}
use only datum instead of datum/1000 except this your code is working fine
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum;
//return Date.parse(strDate);
}
let timestamp = toTimestamp('Sun Jan 19 2020 21:19:40 GMT+0000 (Coordinated Universal Time)');
var d = new Date();
d.setTime(timestamp);
console.log(d.toGMTString()); //Mon, 19 Jan 1970 06:44:28 GMT

How to compare start date and end date in javascript [duplicate]

I have the following string value of a date, Sun Apr 07 2019 00:00:00 GMT-0300, and I need to compare with the following date format 2019-04-08T03:00:00.000Z, note that both are the same day, I need to compare them and return a true as being equal days,
but I do not know how to do this using javascript, any help?
I tried to do something like this but it does not return me true:
if (input) {
//input.value = "Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)"
var situation = angular.isArray(item.situation) ? item.situation : [item.situation];
// situation =
//[
// 0: "2019-04-08T03:00:00.000Z"
// 1: "2019-04-13T03:00:00.000Z"
//]
if (!angular.isArray(input.value)) {
condition = situation.indexOf(input.value) >= 0;
} else if (angular.isArray(input.value)) {
condition = $window._.intersection(situation, input.value).length > 0;
}
}
if (condition) {
break;
}
//input.value = "Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)"
situation =
[
0: "2019-04-08T03:00:00.000Z"
1: "2019-04-13T03:00:00.000Z"
]
Sun Apr 07 2019 00:00:00 GMT-0300
2019-04-08T03:00:00.000Z
note that both are the same day
No, they are not.
You can convert them both to ISO string and just compare their date parts as strings (if I understood the question correctly, you want to compare date only, without time):
function isSameDate(date1, date2) {
const [d1, ] = (new Date(date1)).toISOString().split('T');
const [d2, ] = (new Date(date2)).toISOString().split('T');
return d1 === d2;
}
Convert all the values to Date objects and compare those. Use a framework/library to do it, because parsing strings to dates manually has lots of places where it can go wrong.
Currently you are comparing the literal Strings. Because neither "2019-04-08T03:00:00.000Z", nor "2019-04-13T03:00:00.000Z" match "Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)", your second if statement fails.
Build a date from the strings and compare the days (ie number of seconds since epoch / number of seconds in a day):
const sameDay = (dateString1, dateString2) => {
let time1 = (new Date(dateString1)).getTime();
let time2 = (new Date(dateString2)).getTime();
return Math.floor(Math.abs((time1-time2))/(1000*60*60*24))==0;
}
console.log(
sameDay('Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)','2019-04-08T03:00:00.000Z'),
sameDay('Sun Apr 07 2019 00:00:00 GMT-0300 (Horário Padrão de Brasília)','2019-04-13T03:00:00.000Z'),
);

How can I convert this complicated date format to this in javascript

How can I convert this format "Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)" to just 2014-01-31 in Javascript ?? I know it should be simple but I didnt get it from google
var d = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
var str = $.datepicker.formatDate('yy-mm-dd', d);
alert(str);
http://jsfiddle.net/3tNN8/
This requires jQuery UI.
jsFiddle Demo
Split the string based on the blank spaces. Take the parts and reconstruct it.
function convertDate(d){
var parts = d.split(" ");
var months = {Jan: "01",Feb: "02",Mar: "03",Apr: "04",May: "05",Jun: "06",Jul: "07",Aug: "08",Sep: "09",Oct: "10",Nov: "11",Dec: "12"};
return parts[3]+"-"+months[parts[1]]+"-"+parts[2];
}
var d = "Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)";
alert(convertDate(d));
You can do it like this
var date = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
var year=date.getFullYear();
var month=date.getMonth()+1 //getMonth is zero based;
var day=date.getDate();
var formatted=year+"-"+month+"-"+day;
I see you're trying to format a date. You should totally drop that and use jQuery UI
You can format it like this then
var str = $.datepicker.formatDate('yy-mm-dd', new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
I found Web Developer's Notes helpful in formatting dates
For things like this it's often good to do a little testing in the browser console.
var date = new Date("Fri Jan 31 2014 00:00:00 GMT-0800 (Pacific Standard Time)");
console.log(date.getFullYear() + '-' + date.getMonth()+1 + '-' + date.getDate())
Ensure you add + 1 to the result of getMonth() because it is zero based.
A similar question was asked here:
Where can I find documentation on formatting a date in JavaScript?
start_date="14 Feb 2020";
var new_startDate= new Date(start_date);
var date= moment(new_startDate).format('YYYY-MM-DD');
Answer: 2020-02-14
In here you have to use moment.js
The easiest way to convert is
new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: '2-digit'
}).format(new Date('Your Date'))
Just Replace 'Your Date' with your complicated date format :)
You can also use the Moment.js library, make sure to give it a search.
Few examples: moment().format('MMMM Do YYYY, h:mm:ss a');
moment().format('dddd');
A trifling refinement:
var date = new Date(value);
var year = date.getFullYear();
var rawMonth = parseInt(date.getMonth()) + 1;
var month = rawMonth < 10 ? '0' + rawMonth : rawmonth;
var rawDay = parseInt(date.getDate());
var day = rawDay < 10 ? '0' + rawDay : rawDay;
console.log(year + '-' + month + '-' + day);

Extract specific string and change string format (jquery/ javascript)

I want to extract "10:30" from mystring and convert it to "10, 30".
var mystring = "Sat Dec 17 2011 10:30:00 GMT+0530 (India Standard Time)";
Output will be:
Time: 10:30
Changed Format: 10, 30
Provide code solution only.
You can use this to extract the time:
var mystring = "Sat Dec 17 2011 10:30:00 GMT+0530 (India Standard Time)";
var match = mystring.match(/(\d+:\d+):\d+/);
if (match) {
var output = "Time: " + match[1].replace(":", ", ");
}

javascript add one minute to time object

I have a date string that looks like the following javascript format.
I want to convert this to a date object and add one minute.
timeObject = "Mon Nov 07 2011 06:41:48 GMT-0500 (Eastern Standard Time)";
timeObject.setSeconds(timeObject.getSeconds() + 60);
====== SOLUTION ==========
never mind. I got it...
var time = $('#myDiv').val(); // = "Mon Nov 07 2011 06:41:48 GMT-0500 (Eastern Standard Time)";
var timeObject = new Date(time);
alert(timeObject);
timeObject.setSeconds(timeObject.getSeconds() + 60);
alert(timeObject);
Proper way is:
timeObject.setTime(timeObject.getTime() + 1000 * 60);

Categories

Resources