4

am new to Jquery. I want to insert current date time to Db

var data{
"id":1,
 "name":"mybane",
"datetime":// here i have to put current date time
}

is there any possible way to get the current date time..

Psl
  • 3,830
  • 16
  • 46
  • 84
  • 2
    Have you tried looking anywhere? – DGS Sep 26 '13 at 05:24
  • 1
    possible duplicate of [How to get datetime in javascript?](http://stackoverflow.com/questions/4744299/how-to-get-datetime-in-javascript) – DGS Sep 26 '13 at 05:26

6 Answers6

4

Use this:

var data{
"id":1,
 "name":"mybane",
"datetime":new Date(),
}

Reference: How do I get the current date in JavaScript?

Community
  • 1
  • 1
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
4

You are looking for this. https://stackoverflow.com/a/9050848/829533

All you need to do is to use this output variable in your datetime: param

var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();

var output = d.getFullYear() + '-' +
    ((''+month).length<2 ? '0' : '') + month + '-' +
    ((''+day).length<2 ? '0' : '') + day + ' ' +
    ((''+hour).length<2 ? '0' :'') + hour + ':' +
    ((''+minute).length<2 ? '0' :'') + minute + ':' +
    ((''+second).length<2 ? '0' :'') + second;

In action http://jsfiddle.net/nCE9u/3/

Other then this you can sent the direct your database related function to add current time stamp like the one CURRENT_TIMESTAMP() (in mysql)

Note: you can send this as an string

"datetime" : "CURRENT_TIMESTAMP()"

And give it directly to the database as a string.

Community
  • 1
  • 1
zzlalani
  • 22,960
  • 16
  • 44
  • 73
1

use new Date()

var data{
"id":1,
 "name":"mybane",
"datetime":new Date()
}
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
1
var currentTime = new Date();

var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

var date1 = month + "/" + day + "/" + year; // output

take your required value from below function :

  • getTime() - Number of milliseconds since 1/1/1970 @ 12:00 AM

  • getSeconds() - Number of seconds (0-59)

  • getMinutes() - Number of minutes (0-59)

  • getHours() - Number of hours (0-23)

  • getDay() - Day of the week(0-6). 0 = Sunday, ... , 6 = Saturday

  • getDate() - Day of the month (0-31)

  • getMonth() - Number of month (0-11)

  • getFullYear() - The four digit year (1970-9999)

Shakti Patel
  • 3,762
  • 4
  • 22
  • 29
0
 var d=new Date();
 var time=d.getTime();

This is the function you can use to get current time

Ashisha Nautiyal
  • 1,389
  • 2
  • 19
  • 39
0

Create Date object and format as you need:

var data{
"id":1,
 "name":"mybane",
"datetime":(function(d) { return d.format('dd.mm.yyyy hh:MM') })(new Date());
}
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110