i want to convert utc time to ist time , im getting time like this. i want to remove "GMT+0530 (India Standard Time)"
Thu Jul 19 2018 18:06:14 GMT+0530 (India Standard Time)
Expected result
Jul 19 2018 18:06:14 or 19/7/2018 06:06 PM
sample data
2018-07-19T16:36:21.065Z
code
function UtcToIst(data) {
var dt = new Date(data);
return dt;
}
I achieved it in the following way. Please correct me, if I am wrong.
var data = '2018-07-19T16:36:21.065Z';
function UtcToIst(data) {
var dt = new Date(data);
return dt;
}
var updDate = UtcToIst(data).toLocaleDateString();
var updTime = UtcToIst(data).toLocaleTimeString();
var updDateTime = updDate + ", " + updTime;
console.log(updDateTime); // 7/19/2018, 9:36:21 AM
Related
I have an input date string like so "30/09/1992", and I found this code to suit my need. PFB the code.
var input1 = "30/09/1992";
var isVaidDate = false;
var actualDate = "";
try{
var pattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var arrayDate = input1.match(pattern);
var actualDate = new Date(arrayDate[3], arrayDate[2] - 1, arrayDate[1]);
var isVaidDate = typeof dt.getMonth === 'function';
}catch(e){var output1 = false;}
print(isVaidDate);
print(actualDate);
The above code works fine but when I set the input as "31/09/1992" or "40/09/1992" I am expecting invalid date to come but I get the below output.
for "31/09/1992":
true
Thu Oct 01 1992 00:00:00 GMT+0530 (India Standard Time)
for "40/09/1992":
true
Thu Oct 10 1992 00:00:00 GMT+0530 (India Standard Time)
How should I get this to fail when i pass these two strings. Thanks. Also what is going on and why it didnt fail, would also be useful :)
This example can help you:
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
Here you can verify each variable as day, month and year.
I am displaying current day,month,date,year and time like this
Mon Oct 24 2016 17:09:25 GMT+0530 (India Standard Time)
but i need to display like this
Mon Oct 24 2016 17:09:25
my code in javascript:
var timestamp = new Date();
editor.insertHtml( 'The current date and time is: ' + timestamp.toString());
How can i do this please can anyone tell me how to do this.
Thank you
If you are open to add a library, you should use moment.js
console.log(moment().format('ddd MMM DD YYYY hh:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
If not, a small work around
var d = new Date().toString();
var index = d.lastIndexOf(':') +3
console.log(d.substring(0, index))
Note: moment approach is more preferred
var date = new Date();
var n = d.toLocaleString();
document.getElementById("demo").innerHTML = n;
This is work for me.
var timestamp = new Date();
console.log(
timestamp.toString().split('GMT')
)
// Mon Oct 25 2021 17:56:11 GMT+0530 (India Standard Time)`
the Output will be Mon Oct 25 2021 17:55:02
let today = new Date();
today = today.toString();
today = today.split('G')[0];
console.log(today);
I have been using some extensions on the Date prototype to perform some operations (Advice: do not do that, it screws the date object; too late for me in my current project) and lately have been having some more issues than usual.
To parse strings in H:m format, I made a custom function and assigned it to the Date prototype like this:
Date.__parse = Date.parse;
Date.parse = function(string){
var pattern = /^\d{1,2}:\d{1,2}/ig;
var today = new Date();
if ( pattern.exec(string) ){
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var t = year+"-"+month+"-"+day+" "+string;
var timestamp = Date.__parse(t);
return new Date(timestamp);
}
else{
return new Date(Date.__parse(string));
}
}
Ugly when you try to log the date object but working until recently.
For a time, Date.parse seems to work with dates in "d-m-Y" format, but lately it is returning "invalid date" when I do it.
Did something change in the way major browsers parse the dates, or some change to the specification, or must I assume that the error was there before, and it didn´t trigger "Invalid Date" because I was lucky? (I mostly use the function to validate input fields, so it could very well pass unnoticed).
Guess I will have to write my own date scripts and totally forget the js Date object, it´s really horrible (tried to use moment.js, but the performance in the component where I use it was very poor, that´s why I had to make custom functions).
EDIT
For a better understanding;
What I was doing and seemed to work:
Date.parse("23-7-2016") // Never got an error, expected 23 Jul 2016
What I found after tracking a validation error:
var startDate = Date.parse("23-7-2016");
console.log(startDate.toISOString()); //Got Invalid Date
What I think may have happened
var startDate = Date.parse("12-7-2016");
// expected 12 Jul 2016, got 7 Dec 2016, silently fails, everyone is happy
Why I think the prev. case is not the case: I use an interactive scheduler and performed thousands of tests on it, hardly such an error would go unnoticed.
Worst scenario: chrome updated and changed the way it parses dates.
Not sure... hoped someone could enlighten me.
I've determined your method should work for any valid input. You problem is most likely that your regex is valid for hours above 23, and minutes above 59.
See my jsfiddle, which enumerates all valid inputs. https://jsfiddle.net/kLngLL72/4/
I didn't overwrite the Date.parse function in my example to prevent infinite nesting of the function.
Date.__parse = Date.parse;
var dparse = function(string){
var pattern = /^\d{1,2}:\d{1,2}/ig;
var today = new Date();
if ( pattern.exec(string) ){
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var t = year+"-"+month+"-"+day+" "+string;
var timestamp = Date.__parse(t);
return new Date(timestamp);
}
else{
return new Date(Date.__parse(string));
}
}
$("#data").append("<tr><td>" + dparse("01-01-2016 1:31") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("1-1-2016 0:0") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("1-1-2016 12:59") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("1-1-2016 23:59") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("12-31-2016 1:1") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("12-31-2016") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("12-31-2016 24:0") + "</td></tr>");
$("#data").append("<tr><td>" + dparse("12-31-2016 99:99") + "</td></tr>");
for (var i = 0; i < 24; i++)
{
for (var j = 0; j < 60; j++)
{
$("#data").append("<tr><td>" + dparse("12-31-2016 " + i + ":" + j) + "</td></tr>");
}
}
UPDATED - NEW JS FIDDLE https://jsfiddle.net/mfe55xun/2/
This new example, only passes the hour and minute string in.
Date.__parse = Date.parse;
var dparse = function(string){
var pattern = /^\d{1,2}:\d{1,2}/ig;
var today = new Date();
if ( pattern.exec(string) ){
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var t = year+"-"+month+"-"+day+" "+string;
var timestamp = Date.__parse(t);
return new Date(timestamp);
}
else{
return new Date(Date.__parse(string));
}
}
$("#data").append("<tr><td>" + dparse("99:99") + "</td></tr>");
for (var i = 0; i < 24; i++)
{
for (var j = 0; j < 60; j++)
{
$("#data").append("<tr><td>" + dparse(i + ":" + j) + "</td></tr>");
}
}
UPDATE
It should be noted that if your input string included the date, the regular Date.parse would work on a string with your H:m formatting:
Date.parse("1/2/2016 4:3")
You would just need to append your "4:3" to a current date string, and you could remove your custom Date.parse function.
Another Update For Updated Question
I don't think that format ever worked correctly for you. It has cases where it will work, but it's always going to interpret the day "23rd" to be a month, and give you an invalid date. Here is another jsfiddle example that loops through all conceivable dates in that format, notice only days 1-12 work. https://jsfiddle.net/mfe55xun/6/
Date.__parse = Date.parse;
var dparse = function(string){
var pattern = /^\d{1,2}:\d{1,2}/ig;
var today = new Date();
if ( pattern.exec(string) ){
var year = today.getFullYear();
var month = today.getMonth()+1;
var day = today.getDate();
var t = year+"-"+month+"-"+day+" "+string;
var timestamp = Date.__parse(t);
return new Date(timestamp);
}
else{
return new Date(Date.__parse(string));
}
}
for (var i = 0; i <= 31; i++)
{
for (var j = 0; j <= 12; j++)
{
$("#data").append("<tr><td>" + i + "-" + j + "-2016 = " + dparse(i + "-" + j + "-2016" ) + "</td></tr>");
}
}
Look at the results loop:
test results
0-0-2016 = Invalid Date
0-1-2016 = Invalid Date
0-2-2016 = Invalid Date
0-3-2016 = Invalid Date
0-4-2016 = Invalid Date
0-5-2016 = Invalid Date
0-6-2016 = Invalid Date
0-7-2016 = Invalid Date
0-8-2016 = Invalid Date
0-9-2016 = Invalid Date
0-10-2016 = Invalid Date
0-11-2016 = Invalid Date
0-12-2016 = Invalid Date
1-0-2016 = Invalid Date
1-1-2016 = Fri Jan 01 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-2-2016 = Sat Jan 02 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-3-2016 = Sun Jan 03 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-4-2016 = Mon Jan 04 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-5-2016 = Tue Jan 05 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-6-2016 = Wed Jan 06 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-7-2016 = Thu Jan 07 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-8-2016 = Fri Jan 08 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-9-2016 = Sat Jan 09 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-10-2016 = Sun Jan 10 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-11-2016 = Mon Jan 11 2016 00:00:00 GMT-0500 (Eastern Standard Time)
1-12-2016 = Tue Jan 12 2016 00:00:00 GMT-0500 (Eastern Standard Time)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
The Date.parse() method parses a string representation of a date, and
returns the number of milliseconds since January 1, 1970, 00:00:00 UTC
or NaN if the string is unrecognised or, in some cases, contains
illegal date values (e.g. 2015-02-31).
It is not recommended to use Date.parse as until ES5, parsing of
strings was entirely implementation dependent. There are still many
differences in how different hosts parse date strings, therefore date
strings should be manually parsed (a library can help if many
different formats are to be accommodated).
I found a solution to transform a date like:
Thu Sep 04 2014 00:00:00 GMT+0200 (Romance Daylight Time)
to a format french 04/09/2014
How can i achieve this?
You can do that by using the Date object and its functions:
function zerofy(number){
if(number < 10)
number = "0" + number;
return number;
}
var date = new Date("Thu Sep 04 2014 00:00:00 GMT+0200 (Romance Daylight Time)");
var day = zerofy(date.getDate());
var month = zerofy(date.getMonth());
var year = date.getFullYear();
var result = day + "/" + month + "/" + year
console.log(result);
Working Demo
I would recommend going over this document from MDN for these kind of problems.
Perhaps a basic one-liner would help?
function formatDate(date){
return [('0'+date.getDate()).slice(-2),('0'+date.getMonth()).slice(-2),date.getFullYear()].join('/');
}
I have two variables namely
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
When I compare the two dates I get that date2 is greater which I need is correct. But I do not want to check the time part of the two dates I have. How could I get the date part alone from these two dates and compare it?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}
Try clearing the time using Date.setHours:
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
Example Code:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}
The best way would be to modify the accepted answer's if statement as follows
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
In this way, you can easily check for equality as well because the return type for setHours() is integer.
Try:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}
var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if ((( EndDate - StartDate)/ (86400000*7))<0)
{
alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus();
error = true;
return false;
}