how to covert PST to IST Using javascript - javascript

i get PST date formate from Database and i need to convert IST formate. But my system also in IST Formate. please check this http://jsfiddle.net/sfcdD/9/.
var date = "2012-12-12 05:18:28.541"; // PST date formate fetch from db
var offset = (3600000*(+5.30)); // IST gmtOffset value
var dateformate = 'dd/mm/yyyy "at" h:MM TT';
var dateArray = (date).split(' ');
var year = dateArray[0].split('-');
var time = dateArray[1].split(':');
var d = new Date($.trim(year[0]), $.trim(year[1]-1), $.trim(year[2]), $.trim(time[0]), $.trim(time[1]));
utc = d.getTime() +(d.getTimezoneOffset()*60000); //d.getTimezoneOffset() is taking local timezone
nd = new Date(utc + parseInt(offset));
alert(dateFormat(nd,dateformate)); // dispaly 12/12/2012 at 5:06 AM but need to display 12/12/2012 at 7:48 PM
so my conversion is not work. it display wrong date.

How about this?
var date = "2012-12-12 05:18:28.541";
var offset = (3600000*(+5.50+8));
var dateformate = 'dd/mm/yyyy "at" h:MM TT';
var dateArray = (date).split(' ');
var year = dateArray[0].split('-');
var time = dateArray[1].split(':');
var d = new Date($.trim(year[0]), $.trim(year[1]-1), $.trim(year[2]), $.trim(time[0]), $.trim(time[1]));
var nd = new Date(d.getTime() + offset);
alert(dateFormat(nd,dateformate));

var offset = (3600000*(+5.30)); // IST gmtOffset value
should be
var offset = (3600000*(+5.50)); // IST gmtOffset value

Related

How to convert Date string to a specific time zone

Given a Date string such as var input = '2019-09-19 13:07:09'
How can I convert that string to a different time zone? Knowing that input is in UTC.
I tried the following:
var input = '2019-09-19 13:07:09';
var convertTo = "US/Eastern";
var dateConverted = moment.tz(input, "YYYY-MM-DD hh:mm:ss", convertTo);
dateConverted remains the same as input after that code is executed.
I also tried:
var input = '2019-09-19 13:07:09';
var convertTo = "US/Eastern";
var inUTC = (moment(input).utc());
var dateConverted = moment.tz(inUTC, "YYYY-MM-DD hh:mm:ss", convertTo);
But in this case, the problem is that inUTC becomes Thu Sep 19 2019 20:07:09 GMT+0000 after (moment(input).utc());
I expect the date to be converted to EST which would be 2019-09-19 09:07:09
Any ideas how to solve this? Thanks!
try this :
var input = '2019-09-19 13:07:09';
var offUtc = moment.utc(input, 'YYYY-MM-DD HH:mm:ss');
var convertTo = "US/Eastern";
var dateConverted = offUtc.clone().tz(convertTo);

How to get time using toDate() and not format() [duplicate]

how can i extract time from datetime format.
my datetime format is given below.
var datetime =2000-01-01 01:00:00 UTC;
I only want to get the time 01.00 as 01
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
var time = datetime.toLocaleTimeString();
Update:
The new locales and options arguments let applications specify the
language whose formatting conventions should be used and customize the
behavior of the function. In older implementations, which ignore the
locales and options arguments, the locale used and the form of the
string returned are entirely implementation dependent.
// Depending on timezone, your results will vary
var event = new Date('August 19, 1975 23:15:30 GMT+00:00');
console.log(event.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM
console.log(event.toLocaleTimeString('it-IT'));
// expected output: 01:15:30
What about these methods
new Date().getHours()
new Date().getMinutes()
For example:
var d = new Date();
var n = d.getHours();
Edited
Return the hour, according to universal time:
new Date().getUTCHours()
Example:
var d = new Date();
var n = d.getUTCHours();
As an alternative if you want to get the time from a string -
var datetime ="2000-01-01 01:00:00 UTC";
var myTime = datetime.substr(11, 2);
alert(myTime) //01
with the moment.js library it works in this way:
var result = moment().format('hh');
alert(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
var datetime = ("2000-01-01 01:00:00 UTC");
var d1 = new Date(datetime);
var minute = d1.getUTCMinutes();
var hour = d1.getUTCHours();
if(minute > 0)
alert(hour+"."+minute);
else
alert(hour);
Demo
var date1 = new Date(1945,10,20, 17,30)
var date2 = new Date(1970,1,8, 12,00)
console.log(date1.getHours() - 8 + (date1.getMinutes()/60))
console.log(date2.getHours() - 8 + (date2.getMinutes()/60))
Use the following code:
var datetime = "2000-01-01 01:00:00 UTC";
var dt = new Date(datetime);
var hr = dt.getUTCHours();
if(hr > 12) {
hr -= 12;
}
alert(hr);
refer this link also.
I have done it! It looks like this:
console.log(new Date().toLocaleTimeString() + " " + new Date().getSeconds() + " seconds");
Assuming you have a Date object like
var datetime = new Date("2000-01-01 01:00:00 UTC"); // might not parse correctly in every engine
// or
var datetime = new Date(Date.UTC(2000, 0, 1, 1, 0, 0));
then use the getUTCHours method:
datetime.getUTCHours(); // 1
I hope you can find a solution here.
let preDateTime = new Date("2022-03-31 22:26:00");
let newTime = preDateTime.toLocaleTimeString('en-US');
let hour = newTime.split(":")[0];
let amPm = newTime.split(" ")[1];
let seconds = newTime.split(":")[2].replace(amPm,'');;
let noAmPm = newTime.replace(amPm,'');
let noAmPmSeconds = noAmPm.replace(":"+seconds,'');
let noSeconds = newTime.replace(":"+seconds,' ');
if(parseInt(hour)<9){
newTime = "0"+newTime;
noAmPm = "0"+noAmPm
noSeconds= "0"+noSeconds
noAmPmSeconds = "0"+noAmPmSeconds;
}
console.log(newTime); //10:26:00 PM
console.log(noAmPm); //10:26:00
console.log(noSeconds); //10:26 PM
console.log(noAmPmSeconds); //10:26

one second = 1 year for 12/31/2017 using Utilities.formatDate

Have curious results converting date using Utilities.formatDate()
function test_date_conversion_1() {
var in_d_str = "12/31/2017"
var new_d = Utilities.formatDate(new Date(in_d_str), "EET", "MM/dd/YYYY")
return new_d
}
// new_d = "12/31/2018" 2018?!
I've tried to develop a patch and get unexpected results for this function
function test_date_conversion(offset) {
//offset = 1
var in_d_str = "12/31/2017"
var in_d = new Date(in_d_str)
var in_d_epoch = in_d.getTime()
var in_d_epoch_dep_1 = in_d_epoch+offset
var in_d_d_dep_1 = new Date(in_d_epoch_dep_1)
var new_d = Utilities.formatDate(in_d_d_dep_1, "EET", "MM/dd/YYYY")
}
if offset = 1 then new_d = "12/31/2018"
if offset = (-1) then new_d = "12/30/2017"
so 1 milisecond = 1 year?
EET = GMT+2 = script timezone
worksheet timezone = GMT+3
from debugger i've noticed that date is spoiled by Utilities.formatDate()
I've triede +/-1h, +/-1h+/-1sec offsets and still can't get just 12/31/2017 from 12/31/2017.
I've patched it like this
if (new_d == "12/31/2018")
new_d = "12/31/2017";
but look for solid solution.
thank you for answer in adwance.
The third parameter of formatDate(date, timeZone, format) is
a format per the SimpleDateFormat specification
For the SimpleDateFormat you use Y for the week year and y for the year. Since the week of 12/31/2017 is the first week of 2018, it will show as 2018.
Instead of using "MM/dd/YYYY", use "MM/dd/yyyy".

Get Diffrence between two date string in momentjs?

I am using momentjs for date and I have one date string, ie,"2015-05-10",I want to get date difference from today
var today= moment().format('YYYY-MM-DD');
How it is possible here?
here is a example,
var now = moment(); // moment object of now
var day = moment("2015-05-13"); // moment object of other date
$scope.difference = now.diff(day, 'days'); // calculate the difference in days
$scope.difference = now.diff(day, 'hours'); // calculate the difference in hours
check more options here
here is a example
You can use diff
//Convert to date
var today = moment();
var date = moment("2015-05-13", "YYYY-MM-DD");
//Use diff
var duration = today.diff(date);
var hours = duration.asHours();
if you are talking about time difference in hours
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

extract time from datetime using javascript

how can i extract time from datetime format.
my datetime format is given below.
var datetime =2000-01-01 01:00:00 UTC;
I only want to get the time 01.00 as 01
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
var time = datetime.toLocaleTimeString();
Update:
The new locales and options arguments let applications specify the
language whose formatting conventions should be used and customize the
behavior of the function. In older implementations, which ignore the
locales and options arguments, the locale used and the form of the
string returned are entirely implementation dependent.
// Depending on timezone, your results will vary
var event = new Date('August 19, 1975 23:15:30 GMT+00:00');
console.log(event.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM
console.log(event.toLocaleTimeString('it-IT'));
// expected output: 01:15:30
What about these methods
new Date().getHours()
new Date().getMinutes()
For example:
var d = new Date();
var n = d.getHours();
Edited
Return the hour, according to universal time:
new Date().getUTCHours()
Example:
var d = new Date();
var n = d.getUTCHours();
As an alternative if you want to get the time from a string -
var datetime ="2000-01-01 01:00:00 UTC";
var myTime = datetime.substr(11, 2);
alert(myTime) //01
with the moment.js library it works in this way:
var result = moment().format('hh');
alert(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
var datetime = ("2000-01-01 01:00:00 UTC");
var d1 = new Date(datetime);
var minute = d1.getUTCMinutes();
var hour = d1.getUTCHours();
if(minute > 0)
alert(hour+"."+minute);
else
alert(hour);
Demo
var date1 = new Date(1945,10,20, 17,30)
var date2 = new Date(1970,1,8, 12,00)
console.log(date1.getHours() - 8 + (date1.getMinutes()/60))
console.log(date2.getHours() - 8 + (date2.getMinutes()/60))
Use the following code:
var datetime = "2000-01-01 01:00:00 UTC";
var dt = new Date(datetime);
var hr = dt.getUTCHours();
if(hr > 12) {
hr -= 12;
}
alert(hr);
refer this link also.
I have done it! It looks like this:
console.log(new Date().toLocaleTimeString() + " " + new Date().getSeconds() + " seconds");
Assuming you have a Date object like
var datetime = new Date("2000-01-01 01:00:00 UTC"); // might not parse correctly in every engine
// or
var datetime = new Date(Date.UTC(2000, 0, 1, 1, 0, 0));
then use the getUTCHours method:
datetime.getUTCHours(); // 1
I hope you can find a solution here.
let preDateTime = new Date("2022-03-31 22:26:00");
let newTime = preDateTime.toLocaleTimeString('en-US');
let hour = newTime.split(":")[0];
let amPm = newTime.split(" ")[1];
let seconds = newTime.split(":")[2].replace(amPm,'');;
let noAmPm = newTime.replace(amPm,'');
let noAmPmSeconds = noAmPm.replace(":"+seconds,'');
let noSeconds = newTime.replace(":"+seconds,' ');
if(parseInt(hour)<9){
newTime = "0"+newTime;
noAmPm = "0"+noAmPm
noSeconds= "0"+noSeconds
noAmPmSeconds = "0"+noAmPmSeconds;
}
console.log(newTime); //10:26:00 PM
console.log(noAmPm); //10:26:00
console.log(noSeconds); //10:26 PM
console.log(noAmPmSeconds); //10:26

Categories

Resources