I have the follow function that properly returns the date in the format required, but the value returned isn't respecting the local timezone. In this case, its 4 hours off. What would I need to modify in this function to make it add the proper offsets based on the users location?
Thanks!
function date_str(seconds) {
var dt = new Date(1970, 0, 1);
dt.setSeconds(seconds);
console.log(dt);
var month = dt.getMonth() + 1;
var date = dt.getDate();
var year = dt.getFullYear();
var hours = dt.getHours();
var minutes = dt.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
return month + '/' + date + '/' + year + ', ' + hours + ':' + minutes + ' ' + ampm;
}
Edit: Passing unix time to function 1396450616.505 which converts to Wed, 02 Apr 2014 14:56:56 GMT which returns Sent at 4/2/2014, 2:56 PM from the function itself. The time here is 10:56 AM EST.
Assuming that seconds is a unix epoch (UTC), you should just use
function date_str(seconds) {
var dt = new Date(seconds*1000);
console.log(dt);
…
instead. The get…() methods will respect the local timezone. If you don't want that, you should use the getUTC…() equivalents.
Related
I've searched everywhere but I can't find how to do this. I'm using the Datetime-local input type using:
<input type="datetime-local" />
When a user enters the values, it is in this format on screen:
MM/DD/YYYY HH:MM AM/PM
However when the form is submitted, the datetime-local value appears in this format:
YYYY-MM-DD:THH:MM
I want to keep it in the same format as it's entered. I've searched but can't find a JavaScript that will grab the value for datetime-local and convert it to the MM/DD/YYYY HH:MM AM/PM format and set the time to either AM / PM. Current the time is in Military so anything above 12:59pm will show 13:00 for example. How can the military time also be converted?
You can use this function
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0'+minutes : minutes;
var year = date.getFullYear();
var month = date.getMonth();
month = month < 10 ? '0'+month : month;
var date = date.getDate();
date = date < 10 ? '0' + date : date;
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = month + '/' + date + '/' + year + ' ' + hours + ':' + minutes + ' ' + ampm;
return strTime;
}
console.log(formatDate(new Date));
If you want you can use moment.js in that you can format according to your need, but that library is bit heavy if you worried about your website size.
suppose if time now is 13:36:22 , I want array of past 5 minutes like this
2019-05-07T13:36:00+0530
2019-05-07T13:35:00+0530
2019-05-07T13:34:00+0530
2019-05-07T13:33:00+0530
2019-05-07T13:32:00+0530
I tried something like below to get past 5 minutes by , but dont know how to remove seconds and to get to that format
var now = new Date();
now.setSeconds(0);
var last1 = new Date(today.getTime() - (1000*60));
var last2 = new Date(today.getTime() - (1000*120));
var arr = [last1,last2,..and so on];
but its also returning some seconds like Tue May 07 2019 13:40:57 , I want to remove seconds and also am looking for this format
2019-05-07T13:32:00+0530
You should construct the format yourself, unless you want to use an external library.
First define your timezone offset so you can add it to UTC timestamp returned by Date.now(). Then set s and ms to 0 and build your desired date format. Also added the leading 0 for month, day and so on, which is removed by slice when the number is already double digits.
let offset = 5.5;
let now = Date.now() + offset*60*60*1000;
date = new Date(now);
date.setSeconds(0, 0);
let timezone = Math.floor(offset) === offset ?
"0" + offset + "00" :
"0" + Math.floor(offset) + "30"
dateString = date.getFullYear() +"-"+
("0" + (date.getMonth()+1)).slice(-2) +"-"+
("0" + date.getDate()).slice(-2) +"T"+
("0" + date.getHours()).slice(-2) +":"+
("0" + date.getMinutes()).slice(-2) +":"+
("0" + date.getSeconds()).slice(-2) +"+"+
timezone;
console.log(dateString);
In Javascript how to check current datetime with three dates. i want to disable the radio button if date is pervious datetime.i need to validate the date according to current datetime
suppose a)10/2/2017 11:00 b)21/3/2018 11:20 c)28/4/2018 14:00
above three dates i need to block the radio button and stike the date in javascript
function formatAMPM(date) { // This is to display 12 hour format like
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
var myDate = new Date();
var displayDate = myDate.getMonth()+ '/' +myDate.getDate()+ '/' +myDate.getFullYear()+ ' ' +formatAMPM(myDate);
alert(displayDate);
var dt = Date.parse(displayDate);
var d = Date.parse(ts);
var d1 = Date.parse(ts1);
var d2 = Date.parse(ts2);
alert(d);
alert(dt);
There is a good library momemt you can use that to check whether date is current date. You can use isBefore
Also if you want to use JS only you can do the following:
Convert your date string in Date object and get the time in milliseconds and then compare it to Date.now().
i want get format data like this 201507280945 not like this Thu May 29 2014 13:50:00 GMT-0400 (Eastern Standard Time)
i put this code
var time = new Date();
i get this Thu May 29 2014 13:50:00 GMT-0400 (Eastern Standard Time)
Get the values individually and do concat.
var Current = new Date();
var DD = Current.getDate();
var MM = Current.getMonth() + 1; //January is 0
var YYYY = Current.getFullYear();
var hr = Current.getHours();
var min = Current.getMinutes();
var milsec = Current.getMilliseconds();
and so on.............
How about write your own date formatter.
function dateFormat(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
//below is to format to whatever you want, for example month/day/year time...
return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
var dateString = formatDate(new Date());
console.log(dateString );
In BIRT, i have a column containing a datetime stored as a string. I need to convert these string to datetime format and put the result in another column using Javascript.
The string is the form of: for example: Fri 21 Feb 2014, 09:40 AM.
Hence this when converted to a datetime format and exported to excel, the column should be treat as a date.
Can any one of you help me to do it?
Cheers,
Other answers do not take into consideration this question is in a BIRT context.
Create a computed column in your dataset, with "Date time" as datatype
Enter as expression:
new Date(row["myDateStringField"]);
Where "myDateStringField" is your DateTime column in a String format. Then use this computed column in your report instead of the String column.
That's it!
Checkout momentjs!
You can parse your time of any format like
moment("12-25-1995", "MM-DD-YYYY");
In your case, you don't even have to specify the format. It automatically recognizes it.
And you can output ISO format or convert it to a Javascript Date object.
This is extremely easy to do with javascript. The following code will make a date in a format that Excel will recognize as a date.
http://jsfiddle.net/bbankes/d7SwQ/
var dateString = 'Fri 21 Feb 2014, 09:40 AM';
var date = new Date(dateString);
var yr = date.getFullYear();
var mo = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var hr = hours < 10 ? '0' + hours : hours;
var minutes = date.getMinutes();
var min = (minutes < 10) ? '0' + minutes : minutes;
var seconds = date.getSeconds();
var sec = (seconds < 10) ? '0' + seconds : seconds;
var newDateString = yr + '-' + mo + '-' + day;
var newTimeString = hr + ':' + min + ':' + sec;
var excelDateString = newDateString + ' ' + newTimeString;
If you just want to reformat 'Fri 21 Feb 2014, 09:04 AM' as '2014-02-21 09:04', then the following will do:
function stringToTimestamp(s) {
var t = s.match(/[\d\w]+/g);
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'};
function pad(n){return (n<10?'0':'') + +n;}
var hrs = t[4] % 12;
hrs += /pm$/i.test(t[6])? 12 : 0;
return t[3] + '-' + months[t[2].toLowerCase()] + '-' + pad(t[1]) + ' ' +
pad(hrs) + ':' + pad(t[5]);
}
console.log(stringToTimestamp('Fri 21 Feb 2014, 09:04 AM')); // 2014-02-21 09:04
use the ISO format: YYYY-MM-DDTHH:MM:SS or YYYY-MM-DD
new Date('2011-04-11T11:51:00');
or
new Date('2011-04-11');