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 );
Related
I am facing an issue in javascript dates. i want to do minutes and second from current date.
My code:
date = new Date();
currentdate = new Date();
newCurrent = GetFormattedDate(currentdate);
GetFormattedDate(date) {
hour = ("0" + (date.getHours())).slice(-2);
min = ("0" + (date.getMinutes())).slice(-2);
return hour + ":" + min; //output 18:43
}
expected output:
`18:00` //if minutes are 0 to 29 then show current hours reset the minutes again start with 0 like 18:00
`18:30` //if minutes are 29 to 59 then show current hours reset the minutes again start with 30 like 18:30
What should I do? anyone help me?
var d = new Date();
var myTime = d.getHours() +':'+ (d.getMinutes() <= 29 ? '00' : '30') ;
template string:
const d = new Date();
const myTime = `${d.getHours()}:${d.getMinutes() <= 29 ? '00' : '30'}` ;
or
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var altedMinutes = minutes <= 29 ? '00' : '30';
var displayTime = hours + ':' + altedMinutes;
If you want PM/AM
var suffix = 'AM'
var hours = now.getHours()
if (hours>11) {
hours - =12;
suffix = 'PM'
}
...
var displayTime = hours + ':' + altedMinutes + ' ' + suffix;
How to Covert This time to date time like " 08:45 PM "
json time code
{"time":1480797244,"short":false,"forceseconds":false}
i need covert this time (1480797244) need idea in jQuery or javascript
Use the below method to convert the timestamp to your required format. Check the Updated fiddle also
function formatAMPM(timestamp) {
date = new Date(timestamp * 1000)
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var year = date.getFullYear();
var day = date.getDate();
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 = day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ' ' + ampm;
return strTime;
}
var timeObj = {"time":1480797244,"short":false,"forceseconds":false};
alert(formatAMPM(timeObj.time))
I am trying to format a mysql datetime object in Javascript, but I only get NaN results.
The value from the database is for example this datetime object:
2015-08-27 21:36:03
In my JS I try to convert this object as follows:
var formattedDate = new Date(datetimeObj);
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes;
return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
How come that I when printing the variable, I get NaN/NaN/NaN 12:NaN?
Some browsers will not parse the string "2015-08-27 21:36:03" as a valid date. For best results, use a standard ISO date string, as in
2015-08-27T21:36:03Z
Try this:
<script>
var formattedDate = new Date("2015-08-27 21:36:03");
console.log(formatDate(formattedDate));
function formatDate(date)
{
var hours = date.getHours();
var minutes = date.getMinutes();
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes;
return date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
</script>
Its the same code, just passed the input in your function..
Here you can find answer on your quetions, it something like this with RegEx
var dateString = "2010-08-09 01:02:03";
var reggie = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var dateArray = reggie.exec(dateString);
var dateObject = new Date(
(+dateArray[1]),
(+dateArray[2])-1, // Careful, month starts at 0!
(+dateArray[3]),
(+dateArray[4]),
(+dateArray[5]),
(+dateArray[6])
);
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.
I would like to get this format:
2:18:00 pm
Using the sample code from w3Schools.com below, I can get the correct results from IE and FireFox. But when it comes to Chrome, I get the 24hr clock version where it is simply displayed this way:
14:18:00
In FF
new Date().toLocaleTimeString()
// 2:18:00 pm
function twoDigitPad(number) {
return ("0" + number).slice(-2);
}
function twelveHourTimeString() {
var date = new Date();
var hour = date.getHours();
var min = twoDigitPad(date.getMinutes());
var sec = twoDigitPad(date.getSeconds());
var ampm = hour < 12 ? "am" : "pm";
hour = hour % 12 || 12; // convert to 12-hour format
return hour + ":" + min + ":" + sec + " " + ampm;
}
date.getHours() returns an integer between 0 and 23, which hour % 12 || 12 converts to the 12-hour format.
date.getMinutes() and date.getSeconds() each return an integer, so you'll need to zero-pad those values when they're less than 10. Optionally, hour as well.
Use the following javascript code,
<script type="text/javascript">
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();
if (curr_hour < 12)
{
a_p = "AM";
}
else
{
a_p = "PM";
}
document.write(curr_hour + ":" + curr_min + ":"
+ curr_sec+ a_p);
</script>
The o/p would be as you expect,2:18:00 PM
Dealing with dates reliably cross-browser is a ball-ache in javascript- I would use the DateFormat library; http://blog.stevenlevithan.com/archives/date-time-format
then as he notes on the page, you can call it like so;
dateFormat(now, "h:MM:ss TT");
There are a few alternatives, but this one seems the most light-weight.