String date current date/time?
The date()
function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date
variable, as a string -- no need for any additional operation.
Current date and time as string
Non C++11 solution: With the <ctime>
header, you could use strftime
. Make sure your buffer is large enough, you wouldn't want to overrun it and wreak havoc later.
#include <iostream>
#include <ctime>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
std::string str(buffer);
std::cout << str;
return 0;
}
Getting current date and time in JavaScript
.getMonth()
returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth()
in may will return 4
and not 5
.
So in your code we can use currentdate.getMonth()+1
to output the correct value. In addition:
.getDate()
returns the day of the month <- this is the one you want.getDay()
is a separate method of theDate
object which will return an integer representing the current day of the week (0-6)0 == Sunday
etc
so your code should look like this:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
JavaScript Date instances inherit from Date.prototype. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances
You can make use of the Date
prototype object to create a new method which will return today's date and time. These new methods or properties will be inherited by all instances of the Date
object thus making it especially useful if you need to re-use this functionality.
// For todays date;
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
// For the time now
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
You can then simply retrieve the date and time by doing the following:
var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();
Or call the method inline so it would simply be -
var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();
How do I get a string format of the current date time, in python?
You can use the datetime
module for working with dates and times in Python. The strftime
method allows you to produce string representation of dates and times with a format you specify.
>>> import datetime
>>> datetime.date.today().strftime("%B %d, %Y")
'July 23, 2010'
>>> datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
'10:36AM on July 23, 2010'
How to get the current date/time in Java
It depends on what form of date / time you want:
If you want the date / time as a single numeric value, then
System.currentTimeMillis()
gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Javalong
). This value is a delta from a UTC time-point, and is independent of the local time-zone1.If you want the date / time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:
new Date()
gives you aDate
object initialized with the current date / time. The problem is that theDate
API methods are mostly flawed ... and deprecated.Calendar.getInstance()
gives you aCalendar
object initialized with the current date / time, using the defaultLocale
andTimeZone
. Other overloads allow you to use a specificLocale
and/orTimeZone
. Calendar works ... but the APIs are still cumbersome.new org.joda.time.DateTime()
gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here. (But note that some people report that Joda time has performance issues.; e.g. https://stackoverflow.com/questions/6280829.)in Java 8, calling
java.time.LocalDateTime.now()
andjava.time.ZonedDateTime.now()
will give you representations2 for the current date / time.
Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.
With Java 8 and later, the standard java.time
package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate.3.
1 - System.currentTimeMillis()
gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.
Current Date time in a particular format Python
You can use datetime with strftime to get the desired result. You can do it as -
import datetime
date = datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y")
print(date)
Output :
Mon Jul 06 19:11:33 UTC 2020
Basically the syntax is - datetime.strftime(format)
which will return a string representing date and time using date, time or datetime object. There are many format codes like %Y
, %d
, %m
, etc which you can use to specify the format you want your result to be.
You can read more about them here
How do I get the current date in JavaScript?
Use new Date()
to generate a new Date
object containing the current date and time.
var today = new Date();var dd = String(today.getDate()).padStart(2, '0');var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;document.write(today);
Related Topics
Install Gcc on Linux with No Root Privilege
Acquire/Release Semantics with 4 Threads
C++ Template Metaprogramming - How to Output the Generated Code
Should the Copy-And-Swap Idiom Become the Copy-And-Move Idiom in C++11
Undefined Reference to Google::Protobuf::Internal::Empty_String_[Abi:Cxx11]
How to Find a Particular Value in an Array and Return Its Index
C++11 Rvalue Reference Calling Copy Constructor Too
How to Compress Slot Calls When Using Queued Connection in Qt
Why Is Rvo Disallowed When Returning a Parameter
Is a Linked-List Implementation Without Using Pointers Possible or Not
Should Objects Delete Themselves in C++
When Do Programmers Use Empty Base Optimization (Ebo)
Float Bits and Strict Aliasing
True Isometric Projection with Opengl
How to Expand an Array Dynamically in C++? {Like in Vector }